To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.7% of users enabled 2FA.

Commit aaee9e8d authored by Fangbing Liu's avatar Fangbing Liu
Browse files

code

parents
Requirements:
python>=3.7
tensorflow>=1.15
Datasets:
miniImagenet and tieredImagenet:https://github.com/renmengye/few-shot-ssl-public
CIFAR-FS:https://github.com/bertinetto/r2d2
Train command:
python3 train.py --n_way=5 --n_shot=1 --n_test_way=5 --n_test_shot=1 --lr=1e-3 --step_size=10000 --dataset=mini --exp_name=mini_5w1s_5tw1ts_rn300_k20-tpn --rn=300 --alpha=0.99 --k=20 --encoder=conv --feat_select=no --infer=tpn
from __future__ import print_function
import numpy as np
from PIL import Image
import pickle as pkl
import os
import glob
import csv
class dataset_cifar(object):
def __init__(self, n_examples, n_episodes, split, args):
#self.im_width, self.im_height, self.channels = list(map(int, args['x_dim'].split(',')))
self.im_width, self.im_height, self.channels = [32,32,3]
self.n_examples = n_examples
self.n_episodes = n_episodes
self.split = split
self.ratio = args['ratio']
self.seed = args['seed']
self.root_dir = './data/cifar'
self.n_label = int(self.ratio*self.n_examples)
self.n_unlabel = self.n_examples-self.n_label
self.dataset_l = []
self.dataset_u = []
self.args = args
def build_label_index(self, labels):
label2inds = {}
for idx, label in enumerate(labels):
if label not in label2inds:
label2inds[label] = []
label2inds[label].append(idx)
return label2inds
def load_data_pkl(self):
"""
load the pkl processed mini-imagenet into label,unlabel
"""
pkl_name = '{}/CIFAR_FS_{}.pickle'.format(self.root_dir, self.split)
print('Loading pkl dataset: {} '.format(pkl_name))
try:
with open(pkl_name, "rb") as f:
data = pkl.load(f, encoding='bytes')
image_data = data[b'data']
label_data = data[b'labels']
except:
with open(pkl_name, "rb") as f:
data = pkl.load(f)
image_data = data['data']
label_data = data['labels']
data_classes = self.build_label_index(label_data)
#print(data.keys(), image_data.shape, class_dict.keys())
class_list = sorted(data_classes.keys()) # sorted to keep the order
n_classes = len(class_list)
print('n_classes:{}, n_label:{}, n_unlabel:{}'.format(n_classes,self.n_label,self.n_unlabel))
dataset_l = np.zeros([n_classes, self.n_label, self.im_height, self.im_width, self.channels], dtype=np.float32)
if self.n_unlabel>0:
dataset_u = np.zeros([n_classes, self.n_unlabel, self.im_height, self.im_width, self.channels], dtype=np.float32)
else:
dataset_u = []
for i, cls in enumerate(class_list):
idxs = data_classes[cls]
np.random.RandomState(self.seed).shuffle(idxs) # fix the seed to keep label,unlabel fixed
dataset_l[i] = image_data[idxs[0:self.n_label]]
if self.n_unlabel>0:
dataset_u[i] = image_data[idxs[self.n_label:]]
print('labeled data:', np.shape(dataset_l))
print('unlabeled data:', np.shape(dataset_u))
self.dataset_l = dataset_l
self.dataset_u = dataset_u
self.n_classes = n_classes
del image_data
def next_data(self, n_way, n_shot, n_query, num_unlabel=0, n_distractor=0, train=True):
"""
get support,query,unlabel data from n_way
get unlabel data from n_distractor
"""
support = np.zeros([n_way, n_shot, self.im_height, self.im_width, self.channels], dtype=np.float32)
query = np.zeros([n_way, n_query, self.im_height, self.im_width, self.channels], dtype=np.float32)
if num_unlabel>0:
unlabel = np.zeros([n_way+n_distractor, num_unlabel, self.im_height, self.im_width, self.channels], dtype=np.float32)
else:
unlabel = []
n_distractor = 0
selected_classes = np.random.permutation(self.n_classes)[:n_way+n_distractor]
for i, cls in enumerate(selected_classes[0:n_way]): # train way
# labled data
idx1 = np.random.permutation(self.n_label)[:n_shot + n_query]
support[i] = self.dataset_l[cls, idx1[:n_shot]]
query[i] = self.dataset_l[cls, idx1[n_shot:]]
# unlabel
if num_unlabel>0:
idx2 = np.random.permutation(self.n_unlabel)[:num_unlabel]
unlabel[i] = self.dataset_u[cls,idx2]
for j,cls in enumerate(selected_classes[self.n_classes:]): # distractor way
idx3 = np.random.permutation(self.n_unlabel)[:num_unlabel]
unlabel[i+j] = self.dataset_u[cls,idx3]
support_labels = np.tile(np.arange(n_way)[:, np.newaxis], (1, n_shot)).astype(np.uint8)
query_labels = np.tile(np.arange(n_way)[:, np.newaxis], (1, n_query)).astype(np.uint8)
# unlabel_labels = np.tile(np.arange(n_way+n_distractor)[:, np.newaxis], (1, num_unlabel)).astype(np.uint8)
return support, support_labels, query, query_labels, unlabel
from __future__ import print_function
import numpy as np
from PIL import Image
import pickle as pkl
import os
import glob
import csv
#from scipy.ndimage import imread
#from scipy.misc import imread
#from scipy.misc import imresize
class dataset_mini(object):
def __init__(self, n_examples, n_episodes, split, args):
self.im_width, self.im_height, self.channels = list(map(int, args['x_dim'].split(',')))
self.n_examples = n_examples
self.n_episodes = n_episodes
self.split = split
self.ratio = args['ratio']
self.seed = args['seed']
#self.semi_ratio = args['semi_ratio']
self.root_dir = './data/miniImagenet'
self.n_label = int(self.ratio*self.n_examples)
self.n_unlabel = self.n_examples-self.n_label
self.dataset_l = []
self.dataset_u = []
self.args = args
def load_data(self):
"""
Load data into memory and partition into label,unlabel
"""
print('Loading {} dataset'.format(self.split))
data_split_path = os.path.join(self.root_dir, 'splits', '{}.csv'.format(self.split))
with open(data_split_path,'r') as f:
reader = csv.reader(f, delimiter=',')
data_classes = {}
for i,row in enumerate(reader):
if i==0:
continue
data_classes[row[1]] = 1
data_classes = data_classes.keys()
print(data_classes)
n_classes = len(data_classes)
print('n_classes:{}, n_label:{}, n_unlabel:{}'.format(n_classes,self.n_label,self.n_unlabel))
dataset_l = np.zeros([n_classes, self.n_label, self.im_height, self.im_width, self.channels], dtype=np.float32)
if self.n_unlabel>0:
dataset_u = np.zeros([n_classes, self.n_unlabel, self.im_height, self.im_width, self.channels], dtype=np.float32)
else:
dataset_u = []
for i, cls in enumerate(data_classes):
im_dir = os.path.join(self.root_dir, 'data/{}/'.format(self.split), cls)
im_files = sorted(glob.glob(os.path.join(im_dir, '*.jpg')))
np.random.RandomState(self.seed).shuffle(im_files) # fix the seed to keep label,unlabel fixed
for j, im_file in enumerate(im_files):
im = np.array(Image.open(im_file).resize((self.im_width, self.im_height)),
np.float32, copy=False)
#im = np.array(imresize(imread(im_file), (self.im_width,self.im_height,3))) / 255.0
if j<self.n_label:
dataset_l[i, j] = im
else:
dataset_u[i,j-self.n_label] = im
print('labeled data:', np.shape(dataset_l))
print('unlabeled data:', np.shape(dataset_u))
self.dataset_l = dataset_l
self.dataset_u = dataset_u
self.n_classes = n_classes
def load_data_pkl(self):
"""
load the pkl processed mini-imagenet into label,unlabel
"""
#pkl_name = '{}/data/mini-imagenet-cache-{}.pkl'.format(self.root_dir, self.split)
pkl_name = '{}/mini-imagenet-cache-{}.pkl'.format(self.root_dir, self.split)
#pkl_name = '{}/mini_imagenet_{}.pickle'.format(self.root_dir, self.split)
print('Loading pkl dataset: {} '.format(pkl_name))
try:
with open(pkl_name, "rb") as f:
data = pkl.load(f, encoding='bytes')
image_data = data[b'image_data']
class_dict = data[b'class_dict']
except:
with open(pkl_name, "rb") as f:
data = pkl.load(f)
image_data = data['image_data']
class_dict = data['class_dict']
print(data.keys(), image_data.shape, class_dict.keys())
data_classes = sorted(class_dict.keys()) # sorted to keep the order
n_classes = len(data_classes)
print('n_classes:{}, n_label:{}, n_unlabel:{}'.format(n_classes,self.n_label,self.n_unlabel))
dataset_l = np.zeros([n_classes, self.n_label, self.im_height, self.im_width, self.channels], dtype=np.float32)
if self.n_unlabel>0:
dataset_u = np.zeros([n_classes, self.n_unlabel, self.im_height, self.im_width, self.channels], dtype=np.float32)
else:
dataset_u = []
for i, cls in enumerate(data_classes):
idxs = class_dict[cls]
np.random.RandomState(self.seed).shuffle(idxs) # fix the seed to keep label,unlabel fixed
dataset_l[i] = image_data[idxs[0:self.n_label]]
if self.n_unlabel>0:
dataset_u[i] = image_data[idxs[self.n_label:]]
print('labeled data:', np.shape(dataset_l))
print('unlabeled data:', np.shape(dataset_u))
self.dataset_l = dataset_l
self.dataset_u = dataset_u
self.n_classes = n_classes
del image_data
def next_data(self, n_way, n_shot, n_query, num_unlabel=0, n_distractor=0, train=True):
"""
get support,query,unlabel data from n_way
get unlabel data from n_distractor
"""
support = np.zeros([n_way, n_shot, self.im_height, self.im_width, self.channels], dtype=np.float32)
query = np.zeros([n_way, n_query, self.im_height, self.im_width, self.channels], dtype=np.float32)
if num_unlabel>0:
unlabel = np.zeros([n_way+n_distractor, num_unlabel, self.im_height, self.im_width, self.channels], dtype=np.float32)
else:
unlabel = []
n_distractor = 0
selected_classes = np.random.permutation(self.n_classes)[:n_way+n_distractor]
#selected_classes = [0,12,3,5,10]
for i, cls in enumerate(selected_classes[0:n_way]): # train way
# labled data
idx1 = np.random.permutation(self.n_label)[:n_shot + n_query]
# idx1 = [
# 427,525,383,413,406,160,43,242,306,118,163,93,462,51,466,464,343,230,
# 258,562,557,405,110,28,88,261,205,540,457,301,106,55,131,220,556,308,
# 363,272,461,115,371,32,292,559,76,580,66,201,184,404,263,533,96,539,
# 31,38,508,545,577,97,544,378,235,444,538,561,446,586,128,267,173,497,
# 21,512,84,256,403,321,317,250,436,528,30,527,453,546,236,563,504,241
# ]
# idx1= idx1[:n_shot+n_query]
support[i] = self.dataset_l[cls, idx1[:n_shot]]
query[i] = self.dataset_l[cls, idx1[n_shot:]]
# unlabel
if num_unlabel>0:
idx2 = np.random.permutation(self.n_unlabel)[:num_unlabel]
unlabel[i] = self.dataset_u[cls,idx2]
for j,cls in enumerate(selected_classes[self.n_classes:]): # distractor way
idx3 = np.random.permutation(self.n_unlabel)[:num_unlabel]
unlabel[i+j] = self.dataset_u[cls,idx3]
support_labels = np.tile(np.arange(n_way)[:, np.newaxis], (1, n_shot)).astype(np.uint8)
query_labels = np.tile(np.arange(n_way)[:, np.newaxis], (1, n_query)).astype(np.uint8)
# unlabel_labels = np.tile(np.arange(n_way+n_distractor)[:, np.newaxis], (1, num_unlabel)).astype(np.uint8)
# #semi-supervised
# #support_labels [n_way,n_shot]
# if self.semi_ratio > 0:
# support_labels[:, int(n_shot * self.semi_ratio):] = -1
return support, support_labels, query, query_labels, unlabel
from __future__ import print_function
import numpy as np
import pickle as pkl
import os
from tqdm import tqdm
#import cv2
from PIL import Image as pil_image
import io
class dataset_tiered(object):
def __init__(self, n_examples, n_episodes, split, args):
self.im_width, self.im_height, self.channels = list(map(int, args['x_dim'].split(',')))
self.n_examples = n_examples
self.n_episodes = n_episodes
self.split = split
self.ratio = args['ratio']
self.seed = args['seed']
self.root_dir = './data/tieredImagenet'
self.iamge_data = []
self.dict_index_label = []
self.dict_index_unlabel = []
def load_data_pkl(self):
"""
load the pkl processed tieredImagenet into label,unlabel
maintain label,unlabel data dictionary for indexes
"""
labels_name = '{}/{}_labels.pkl'.format(self.root_dir, self.split)
# images_name = '{}/{}_images_png.pkl'.format(self.root_dir, self.split)
images_name = '{}/{}_images.npz'.format(self.root_dir, self.split)
print('labels:',labels_name)
print('images:',images_name)
# decompress images if npz not exits
if not os.path.exists(images_name):
png_pkl = images_name[:-4] + '_png.pkl'
if os.path.exists(png_pkl):
decompress(images_name, png_pkl)
else:
raise ValueError('path png_pkl not exits')
if os.path.exists(images_name) and os.path.exists(labels_name):
try:
with open(labels_name, 'rb') as f:
data = pkl.load(f)
label_specific = data["label_specific"]
#label_general = data["label_general"]
#label_specific_str = data["label_specific_str"]
#label_general_str = data["label_general_str"]
except:
with open(labels_name, 'rb') as f:
data = pkl.load(f, encoding='bytes')
label_specific = data[b'label_specific']
#label_general = data[b"label_general"]
#label_specific_str = data[b"label_specific_str"]
#label_general_str = data[b"label_general_str"]
print('read label data:{}'.format(len(label_specific)))
labels = label_specific
# with open(images_name, 'rb') as f:
# array = pkl.load(f, encoding='bytes')
# image_data = np.zeros([len(array), 84, 84, 3], dtype=np.uint8)
# for ii, item in tqdm(enumerate(array), desc='decompress'):
# #im = cv2.imdecode(item, 1)
# im = np.array(pil_image.open(io.BytesIO(item)))
# image_data[ii] = im
with np.load(images_name, mmap_mode="r", encoding='latin1') as data:
image_data = data["images"]
print('read image data:{}'.format(image_data.shape))
n_classes = np.max(labels)+1
print('n_classes:{}, n_label:{}%, n_unlabel:{}%'.format(n_classes,self.ratio*100,(1-self.ratio)*100))
dict_index_label = {} # key:label, value:idxs
dict_index_unlabel = {}
for cls in range(n_classes):
idxs = np.where(labels==cls)[0]
nums = idxs.shape[0]
np.random.RandomState(self.seed).shuffle(idxs) # fix the seed to keep label,unlabel fixed
n_label = int(self.ratio*nums)
n_unlabel = nums-n_label
dict_index_label[cls] = idxs[0:n_label]
dict_index_unlabel[cls] = idxs[n_label:]
self.image_data = image_data
self.dict_index_label = dict_index_label
self.dict_index_unlabel = dict_index_unlabel
self.n_classes = n_classes
print(dict_index_label[0])
print(dict_index_unlabel[0])
def next_data(self, n_way, n_shot, n_query, num_unlabel=0, n_distractor=0, train=True):
"""
get support,query,unlabel data from n_way
get unlabel data from n_distractor
"""
support = np.zeros([n_way, n_shot, self.im_height, self.im_width, self.channels], dtype=np.float32)
query = np.zeros([n_way, n_query, self.im_height, self.im_width, self.channels], dtype=np.float32)
if num_unlabel>0:
unlabel = np.zeros([n_way+n_distractor, num_unlabel, self.im_height, self.im_width, self.channels], dtype=np.float32)
else:
unlabel = []
n_distractor = 0
selected_classes = np.random.permutation(self.n_classes)[:n_way+n_distractor]
for i, cls in enumerate(selected_classes[0:n_way]): # train way
# labled data
idx = self.dict_index_label[cls]
np.random.RandomState().shuffle(idx)
idx1 = idx[0:n_shot + n_query]
support[i] = self.image_data[idx1[:n_shot]]
query[i] = self.image_data[idx1[n_shot:]]
# unlabel
if num_unlabel>0:
idx = self.dict_index_unlabel[cls]
np.random.RandomState().shuffle(idx)
idx2 = idx[0:num_unlabel]
unlabel[i] = self.image_data[idx2]
for j,cls in enumerate(selected_classes[self.n_classes:]): # distractor way
idx = self.dict_index_unlabel[cls]
np.random.RandomState().shuffle(idx)
idx3 = idx[0:num_unlabel]
unlabel[i+j] = self.image_data[idx3]
support_labels = np.tile(np.arange(n_way)[:, np.newaxis], (1, n_shot)).astype(np.uint8)
query_labels = np.tile(np.arange(n_way)[:, np.newaxis], (1, n_query)).astype(np.uint8)
# unlabel_labels = np.tile(np.arange(n_way+n_distractor)[:, np.newaxis], (1, num_unlabel)).astype(np.uint8)
return support, support_labels, query, query_labels, unlabel
#def compress(path, output):
# with np.load(path, mmap_mode="r") as data:
# images = data["images"]
# array = []
# for ii in tqdm(six.moves.xrange(images.shape[0]), desc='compress'):
# im = images[ii]
# im_str = cv2.imencode('.png', im)[1]
# array.append(im_str)
# with open(output, 'wb') as f:
# pkl.dump(array, f, protocol=pkl.HIGHEST_PROTOCOL)
#
#
def decompress(path, output):
with open(output, 'rb') as f:
array = pkl.load(f, encoding='bytes')
images = np.zeros([len(array), 84, 84, 3], dtype=np.uint8)
for ii, item in tqdm(enumerate(array), desc='decompress'):
# im = cv2.imdecode(item, 1)
im = np.array(pil_image.open(io.BytesIO(item)))
images[ii] = im
np.savez(path, images=images)
# coding: utf-8
from __future__ import print_function
import numpy as np
#import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import tensorflow.compat.v1.keras.layers as layers
class models(object):
def __init__(self, args):
# parameters
self.im_width, self.im_height, self.channels = list(map(int, args['x_dim'].split(',')))
self.h_dim, self.z_dim = args['h_dim'], args['z_dim']
self.args = args
#self.im_size = np.ceil(self.im_width / 16, dtype=int)
# placeholders for data and label
self.x = tf.placeholder(tf.float32, [None, None, self.im_height, self.im_width, self.channels])
self.ys = tf.placeholder(tf.int64, [None, None])
self.q = tf.placeholder(tf.float32, [None, None, self.im_height, self.im_width, self.channels])
self.y = tf.placeholder(tf.int64, [None, None])
self.phase = tf.placeholder(tf.bool, name='phase')
self.alpha = args['alpha']
# self.resnet = ResNet12(conv_cut = True)
def resnet_block(self, x, kernel_size, filters, conv_cut=True, name = 'resblock', reuse=False):
with tf.variable_scope(name, reuse=reuse):
x_ori = x
#conv1
out = tf.layers.conv2d(x, filters, kernel_size, padding='SAME', name=name + '_1_conv', kernel_initializer='he_normal', kernel_regularizer = tf.keras.regularizers.l2(0.01), use_bias = False)
out = tf.layers.batch_normalization(out, epsilon=1.001e-5, name=name + '_1_bn', training = self.phase)
out = tf.nn.leaky_relu(out)
#conv2 = tf.layers.conv2d(filters, kernel_size, padding='SAME', name=name + '_2_conv')
out = tf.layers.conv2d(out, filters, kernel_size, padding='SAME', name=name + '_2_conv', kernel_initializer='he_normal', kernel_regularizer = tf.keras.regularizers.l2(0.01), use_bias = False)
out = tf.layers.batch_normalization(out, epsilon=1.001e-5, name=name + '_2_bn', training = self.phase)
out = tf.nn.leaky_relu(out)
#conv3
out = tf.layers.conv2d(out, filters, kernel_size, padding='SAME', name=name + '_3_conv', kernel_initializer='he_normal', kernel_regularizer = tf.keras.regularizers.l2(0.01), use_bias = False)
out = tf.layers.batch_normalization(out, epsilon=1.001e-5, name=name + '_3_bn', training = self.phase)
#short cut
if conv_cut:
shortcut = tf.layers.conv2d(x_ori, filters, 1, name=name + '_0_conv', kernel_initializer='he_normal', kernel_regularizer = tf.keras.regularizers.l2(0.01), use_bias = False)
shortcut = tf.layers.batch_normalization(shortcut, epsilon=1.001e-5, name=name + '_0_bn', training = self.phase)
else:
shortcut = x_ori
out += shortcut
out = tf.nn.leaky_relu(out)
out = tf.layers.MaxPooling2D(pool_size = (2, 2), strides=2)(out)
return out
def resnet(self, x, channel_list = [64,128,256,64], conv_cut=True, reuse=False):
with tf.variable_scope('resnet', reuse=reuse):
x = self.resnet_block(x, kernel_size = 3, filters = channel_list[0], conv_cut = conv_cut, name = 'resblock1')
x = self.resnet_block(x, kernel_size = 3, filters = channel_list[1], conv_cut = conv_cut, name = 'resblock2')
x = self.resnet_block(x, kernel_size = 3, filters = channel_list[2], conv_cut = conv_cut, name = 'resblock3')
x = self.resnet_block(x, kernel_size = 3, filters = channel_list[3], conv_cut = conv_cut, name = 'resblock4')
return x
def conv_block(self, inputs, out_channels, pool_pad='VALID', name='conv'):
with tf.variable_scope(name):
conv = tf.layers.conv2d(inputs, out_channels, kernel_size=3, padding="same")
#conv = tf.contrib.layers.batch_norm(conv, is_training=self.phase, decay=0.999, epsilon=1e-3, scale=True, center=True)
conv = tf.layers.batch_normalization(conv, epsilon=1e-3, training = self.phase)
conv = tf.nn.relu(conv)
#conv = tf.contrib.layers.max_pool2d(conv, 2, padding=pool_pad)
conv = tf.layers.MaxPooling2D(pool_size = (2, 2), strides=2, padding = pool_pad)(conv)
return conv
def encoder(self, x, h_dim, z_dim, reuse=False):
"""Feature embedding network"""
with tf.variable_scope('encoder', reuse=reuse):
net = self.conv_block(x, h_dim, name='conv_1')
net = self.conv_block(net, h_dim, name='conv_2')
net = self.conv_block(net, h_dim, name='conv_3')
net = self.conv_block(net, z_dim, name='conv_4')
#net = tf.contrib.layers.flatten(net)
net = tf.keras.layers.Flatten()(net)
return net
def feat_select(self, x, reuse=False):
"""Feature embedding network"""
with tf.variable_scope('feat_select', reuse=reuse):
#----
num_filter = 32
N = self.args['n_way']*(self.args['n_shot'] + self.args['n_query'])
net = tf.reshape(x, [N,self.args['im_size'],self.args['im_size'],64])#[N,5,5,64]
feat_val = tf.transpose(net, [1,2,0,3])#[5,5,N,64]
feat_val = tf.reshape(feat_val, [self.args['im_size']*self.args['im_size'],N,64,1])
feat_val = tf.keras.layers.Conv2D(filters=64, kernel_size=(N,1), strides=(1,1),padding='valid',activation=tf.nn.leaky_relu, use_bias=False)(feat_val)#[25,1,64,64]
feat_val = tf.keras.layers.Conv2D(filters=32, kernel_size=(1,1), strides=(1,1), padding='valid',activation=tf.nn.leaky_relu, use_bias=False)(feat_val)#[25,1,64,32]
feat_val = tf.keras.layers.Conv2D(filters=1, kernel_size=(1,1), strides=(1,1), padding='valid',activation=tf.nn.leaky_relu, use_bias=False)(feat_val)#[25,1,64,1]
feat_val = tf.reshape(feat_val, [self.args['im_size'],self.args['im_size'],-1])
#feat_val = tf.nn.softmax(feat_val)
net = tf.math.multiply(net, feat_val)
net = tf.reshape(net, [N, self.args['im_size']*self.args['im_size']*64])
return net, feat_val
#----
def conv_feat(self, net, N, win_size, name, <