def __init__(self, name, input_shape, n_classes, momentum=0.9, n_features=4096, batch_size=32, learning_rate=0.01): """ name: string, name of model using for saving graph input_shape: tuple, shape of image n_classes: int, number of classes momentum: float, use for momentum optimizer n_features: int, size of embedded features batch_size: int, number of images of a batch learning_rate: float, learning rate of opitmizer """ Resnet.__init__(self, name=name, input_shape=input_shape, n_classes=n_classes, n_features=n_features, batch_size=batch_size, learning_rate=learning_rate) self.momentum = momentum
def __init__(self, name, input_shape, n_classes, margin=0.5, threshold=0.5, momentum=0.9, n_features=4096, batch_size=32, learning_rate=1e-3): """ name: string, name of model using for saving graph input_shape: tuple, shape of image n_classes: int, number of classes margin: float, margin between negative and possitive distance threshold: float, threshold for deciding 2 images are in the same class or not momentum: float, use for momentum optimizer n_features: int, size of embedded features batch_size: int, number of images of a batch learning_rate: float, learning rate of opitmizer """ Resnet.__init__(self, name=name, input_shape=input_shape, n_classes=n_classes, n_features=n_features, batch_size=batch_size, learning_rate=learning_rate) self.margin = margin self.threshold = threshold self.momentum = momentum
def run(self): sys.stdout = Logger("train_record.txt") start = time.clock() if self.type_name == 'HA': self.l_name1 = [ 'H1', 'H3', 'H4', 'H5', 'H6', 'H7', 'H9', 'H10', 'H11', 'H2', 'H13', 'H16' ] self.l_name2 = ['H2', 'H13', 'H16'] # self.num_classes = 12 elif self.type_name == 'NA': self.l_name1 = ['N1', 'N2', 'N3', 'N6', 'N7', 'N8', 'N9', 'N5'] self.l_name2 = ['N5'] # self.num_classes = 8 # if self.multi==0: for l in self.l_name1: sequences, label, max_len, num_acid = load_dataset( l, self.type_name) # split train and valid data x_train, x_val, y_train, y_val = train_test_split(sequences, label, test_size=0.2) x_val = x_val.reshape(x_val.shape[0], max_len, num_acid, 1) x_train = x_train.reshape(x_train.shape[0], max_len, num_acid, 1) print('x_val shape is', x_val.shape) print('x_train shape is', x_train.shape) # make a history object history = LossHistory() resnet = Resnet(l, history, self.args) model = resnet.set_model(max_len, num_acid) if l in self.l_name2: model.load_weights('./result/' + self.type_name + '/weight/model_weights_' + self.best_l + '_8:2.h5') resnet.run(model, x_train, x_val, y_train, y_val) if l in self.l_name1: if self.min_loss > history.val_loss['epoch'][-1]: self.min_loss = history.val_loss['epoch'][-1] self.best_l = l print('the training is ok!') end = time.clock() print('Time cost is:', end - start) history.acc_plot('epoch', l, self.type_name) history.loss_plot('epoch', l, self.type_name) '''
def prepare_Resnet(output_dim, sess=None, inputT=None, input_dim=None, checkpoint_dir=None, reuse=False): print(" [*] Preparing Resnet ... ") if inputT is not None: model = Resnet(inputT=inputT, output_dim=output_dim, reuse=reuse) elif input_dim is not None: inputT = tf.placeholder(tf.float32, [None, input_dim, input_dim, 3]) # RGB by default model = Resnet(inputT=inputT, output_dim=output_dim, reuse=reuse) else: raise Exception("Either inputT should be provided or input_dim should be specified!") if checkpoint_dir and sess: # scope = tf.get_default_graph().get_name_scope() # print(scope) # # trainable_variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=scope) # map = {} # for idx, var in enumerate(trainable_variables): # if scope in var.name: # name = var.name[len(scope)+1:-2] # map[name] = var # saver = tf.train.Saver(var_list=map) print(" [*] Reading checkpoints...") saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(checkpoint_dir) if ckpt and ckpt.model_checkpoint_path: ckpt_name = os.path.basename(ckpt.model_checkpoint_path) saver.restore(sess, os.path.join(checkpoint_dir, ckpt_name)) print(" [*] Success to read {}".format(ckpt_name)) else: print(" [*] Failed to find a checkpoint") return inputT, model
def __init__(self, device): super().__init__() self.clean_img_train = None self.noised_img_train = None self.output = None self.loss = None model = Resnet().cuda() criterion = nn.MSELoss() optimizer = torch.optim.Adam(model.parameters(), lr=self.learning_rate, weight_decay=1e-5) train_load = Dataset.CIFAR_train_loader() for epoch in range(self.num_epochs): for i, data in enumerate(train_load): self.clean_img_train, _ = data[0], data[1] self.noised_img_train = torch.tensor(random_noise(self.clean_img_train, mode='s&p', salt_vs_pepper=0.5, clip=True)) random_noise(self.clean_img_train, mode='s&p', salt_vs_pepper=0.5, clip=True) # fixing "RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same" self.clean_img_train, self.noised_img_train = self.clean_img_train.to(device), self.noised_img_train.to(device) output = model(self.noised_img_train) self.loss = criterion(output, self.clean_img_train) # backward self.loss.backward() optimizer.step() optimizer.zero_grad() # period of the number of photos in dateset if i == self.num_train_images_in_epoch: break # log print(f'epoch [{epoch + 1}/{self.num_epochs}], loss:{self.loss.item():.4f}') # saving image during learning if epoch % 10 == 0 and self.saving_image_during_learning: combined_img = torch.cat((self.clean_img_train, self.noised_img_train, self.output), 3) #combining images image_save(combined_img, f"./{epoch}_all_vs1.png") torch.save(model, self.Resnet_model_save_PATH)
def get_cnn(architecture='resnet18', embedding_dim=300): if architecture == 'resnet18': cnn = Resnet(embedding_dim=embedding_dim) elif architecture == 'resnet152': cnn = Resnet152(embedding_dim=embedding_dim) elif architecture == 'alexnet': cnn = Alexnet(embedding_dim=embedding_dim) elif architecture == 'vgg': cnn = Vgg(embedding_dim=embedding_dim) elif architecture == 'inception': cnn = Inception(embedding_dim=embedding_dim) elif architecture == 'squeeze': cnn = SqueezeNet(embedding_dim=embedding_dim) elif architecture == 'dense': cnn = DenseNet(embedding_dim=embedding_dim) return cnn
dev_dataloader = DataLoader(dev_dir, transform) dev_data = dev_dataloader.gen_data() toc = time.time() print('dev_data loaded %.2f' % ((toc - tic) / 60)) embedding_dim = 64 # model_name = 'alexnet' # model_name = 'resnet' model_name = 'resnet152' if not os.path.exists(model_name): os.makedirs(model_name) if model_name == 'alexnet': cnn = Alexnet(embedding_dim=embedding_dim, pretrained=False) elif model_name == 'resnet': cnn = Resnet(embedding_dim=embedding_dim, pretrained=False) elif model_name == 'resnet152': cnn = Resnet152(embedding_dim=embedding_dim, pretrained=False) triplet_net = TripletNet(cnn) gpu_device = 1 if torch.cuda.is_available(): with torch.cuda.device(gpu_device): triplet_net.cuda() num_epochs = 100000 minibatch_size = 8 learning_rate = 1e-4 params = triplet_net.parameters() optimizer = torch.optim.Adam(params, lr=learning_rate)
def train(model, epoch=EPOCH, batch_size=16, dev=False, checkpoint_path='tmp'): tf.reset_default_graph() # graph y_, train_flag_, image_, width_, height_ = input_holder() image = image_test_stream(image_, width_, height_) x_ = tf.placeholder_with_default(image, (None, 224, 224, CHANEL), name='x_train') print("开始重建网络resnet") start = time.time() #logits = resnet(x_, is_train=train_flag_) resnet_obj = Resnet(input_x=x_, input_y=y_, is_train=True) cost = resnet_obj.cost acc = resnet_obj.acc step = resnet_obj.step output = resnet_obj.output end = time.time() print("重建网络resnet时间为:%f秒" % (end - start)) min_pred = tf.reduce_min(output) pred = tf.identity(min_pred, name='pred') which_box = tf.argmin(output) which_box = tf.cast(which_box, dtype=tf.int32) row = which_box // 6 column = which_box[0] % 6 points = [ width_ * column / 6, height_ * row / 3, width_ * (column + 1) / 6, height_ * (row + 1) / 3 ] points = tf.identity(points, name='points') train_x, train_y, valid_x, valid_y, test_x, test_y = process_data_set() sess = tf.InteractiveSession() sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) saver = tf.train.Saver(max_to_keep=20, ) counter = 0 start_time = time.clock() current_time = time.clock() cost_list = [] acc_list = [] extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) best_valid_acc = 0 saver.save(sess, './checkpoints/' + checkpoint_path + 'zxq2max') with tf.device("/gpu:0"): for i in range(epoch): for x_batch, y_batch in gen_batch(train_x, train_y, batch_size): #print("counter=%d"%counter) if counter % 100 == 0: val_cost_list = [] val_acc_list = [] for test_x_batch, test_y_batch in gen_batch( test_x, test_y, batch_size): step_test_cost, step_test_acc = sess.run( [cost, acc], feed_dict={ x_: test_x_batch, y_: test_y_batch, train_flag_: False }) val_cost_list.append(step_test_cost) val_acc_list.append(step_test_acc) test_cost = np.mean(val_cost_list) test_acc = np.mean(val_acc_list) if best_valid_acc < test_acc: resnet_obj.save_model(sess) best_valid_acc = test_acc saver.save( sess, './checkpoints/' + checkpoint_path + 'zxq2max') time_per_step = time.clock() - current_time seconds = time_per_step * (epoch - i) * len(train_x) / ( 100 * batch_size) m, s = divmod(seconds, 60) h, m = divmod(m, 60) eta = "%02d:%02d:%02d" % (h, m, s) current_time = time.clock() print( 'Step:{:>4d}, ETA:{}, Test_cost:{:.5F},best_valid_acc:{:.5F} ,Test_acc:{:.4F}' .format(counter, eta, test_cost, best_valid_acc, test_acc)) if counter % 500 == 0 and counter > 0: # saver.save(sess,'./checkpoints/'+checkpoint_path,global_step=i) print('checkpoint save as ' + checkpoint_path + str(i)) _, __, step_cost, step_acc, _ = sess.run([ step, extra_update_ops, cost, acc, resnet_obj.maintain_averages_op ], feed_dict={ x_: x_batch, y_: y_batch, train_flag_: True }) cost_list.append(step_cost) acc_list.append(step_acc) counter = counter + 1 if dev and counter % 10 == 0: update_op = [ resnet_obj.mean_list[0], resnet_obj.ema.average(resnet_obj.mean_list[0]), resnet_obj.learning_rate ] mean1, sh_mean1, learn_rate = sess.run(update_op, feed_dict={ x_: x_batch, y_: y_batch, train_flag_: False }) cur_acc = np.mean(acc_list[-10:]) cur_cost = np.mean(cost_list[-10:]) print("Step: ", counter, " Cost: ", cur_cost, " Accuracy: ", cur_acc, "learn_rate=", learn_rate) #print("counter=%d" % counter) sess.close() show_train(acc_list)
def __init__(self, inputT, output_dim, num_logits=100, reuse=False): print(" [*] Constructing GBP_End2End ... ") """ GBP Defense :param inputT: 4D tensor, has to be provided :param output_dim: has to be provided :param num_logits: the num of logits of the shallow CNN used for the GBP reconstruction """ self.inputT = inputT self.output_dim = output_dim self.num_logits = num_logits self.reuse = reuse print("Output dim = {}".format(self.output_dim)) print("Reuse = {}, (T)Testing/(F)Training".format(self.reuse)) self.layers_dic = {} self.layers_dic['GBP_End2End_input'] = self.inputT with tf.variable_scope("GBP_End2End") as scope: if self.reuse: scope.reuse_variables() eval_graph = tf.get_default_graph() # get the graph # Construct the shallow CNN used for the GBP reconstruction # The gradient has to be overwritten # The gradient registration happens in Prepare_Model with eval_graph.gradient_override_map({'Relu': 'GuidedRelu'}): self.NN1 = Shallow_CNN(self.inputT, output_dim=self.num_logits, reuse=self.reuse) ##################################### GBP Reconstruction ############################################### logits = self.NN1.logits # get the logits # randomly pick one logit index = tf.random_uniform([1], minval=0, maxval=self.num_logits, dtype=tf.int32)[0] # raw gbp reconstruction tfOp_gbp_raw = tf.gradients(logits[:, index], self.inputT)[0] # normalizations tfOp_gbp_submin = tf.map_fn(lambda img: img - tf.reduce_min(img), tfOp_gbp_raw) tfOp_gbp_divmax = tf.map_fn(lambda img: img / tf.reduce_max(img), tfOp_gbp_submin) # just a port for the visualization if necessary tfOp_gbp_255 = tf.map_fn(lambda img: tf.cast(img * 255, tf.int32), tfOp_gbp_divmax, dtype=tf.int32) ##################################### GBP Reconstruction ############################################### # now use a Resnet to classify these GBP reconstructions self.NN2 = Resnet(tfOp_gbp_divmax, output_dim=self.output_dim, reuse=self.reuse) self.phase = self.NN2.phase self.kp = self.NN2.kp self.gbp_reconstructions = tfOp_gbp_255 # an output port for visualizing GBP reconstructions self.logits = self.NN2.logits self.probs = tf.nn.softmax(self.logits) self.layers_dic.update(self.NN1.layers_dic) self.layers_dic.update(self.NN2.layers_dic)
def run(self): ''' if self.multi==1: sequences, label, max_len, num_acid = load_multidata(self.type_name, isTrain=False) seq_test = sequences.reshape(sequences.shape[0], max_len, num_acid, 1) print('the shape of test seqs is', seq_test.shape) print('label is', label) history = LossHistory() res = Resnet('test', history, self.args) model = res.set_multimodel(max_len, num_acid) model.load_weights('./result/'+self.type_name+'/weight/multi_model_weights_8:2.h5') result = model.predict(seq_test, batch_size=64, verbose=0) predict = [-1 for i in range(len(result))] print('result is:', result) for i in range(len(result)): predict[i] = np.argmax(result[i]) print('predict is:', predict) # evaluate accuracy = accuracy_score(label, predict) print('accuracy of test is', accuracy) print('class accuracy is:\n',classification_report(label, predict)) ''' result_final = [] predict = [] if self.type_name == 'HA': self.label_list = [ 'H1', 'H10', 'H11', 'H13', 'H16', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H9' ] elif self.type_name == 'NA': self.label_list = ['N1', 'N2', 'N3', 'N5', 'N6', 'N7', 'N8', 'N9'] label_dic = labels_dic(self.label_list) sequences, label, max_len, num_acid = load_dataset('test', self.type_name, isTrain=False) seq_test = sequences.reshape(sequences.shape[0], max_len, num_acid, 1) print('the shape of test seqs is', seq_test.shape) print('label is', label) print('set label is', set(label)) history = LossHistory() res = Resnet('test', history, self.args) model = res.set_model(max_len, num_acid) # for each subtype, calculate voting score for l_name in self.label_list: print('current l_name is', l_name) model.load_weights('./result/' + self.type_name + '/weight/model_weights_' + l_name + '_8:2.h5') result = model.predict(seq_test, batch_size=64, verbose=0) result_final.append(result) # print('result is', result) # print('the shape of result is', result.shape) result_final = np.array(result_final) print('final result shape is', result_final.shape) # print('final result is', result_final) # for each test example, cal the predict label for i in range(result_final.shape[1]): classes = [] score = [] # for each classifer, cal the possibility for j in range(len(self.label_list)): if result_final[j][i] > 0.5: classes.append(j) score.append(result_final[j][i]) # print('score is', score) # print('classes shape is', len(classes)) if len(classes) == 0: classes.append(-1) if len(classes) == 1: predict.append(classes[0]) else: predict.append(max_class(score, classes)) print('predict is', predict) predict = np.array(predict) print('set predict is', set(predict)) # evaluate accuracy = accuracy_score(label, predict) print('accuracy of test is', accuracy) print('class accuracy is:\n', classification_report(label, predict))
# # root/cat/123.png # # root/cat/nsdf3.png # # root/cat/asd932_.png ###################### ##############数据装载############### train_loader = torch.utils.data.DataLoader( dataset=train_dataset, # 装载数据 batch_size=args.batch_size, # 设置批大小 shuffle=True) # 是否随机打乱 test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=args.batch_size, shuffle=True) #############模型载入################# Resnet = Resnet() if not args.no_cuda: print('正在使用gpu') Resnet.cuda() print(Resnet) ###############损失函数################## criterion = nn.CrossEntropyLoss() # 内置标准损失 optimizer = torch.optim.Adam(Resnet.parameters(), lr=args.lr) # Adam优化器 #############训练过程##################### total_loss = 0 #内存循环使用 for epoch in range(args.epochs): for i, (images, labels) in enumerate(train_loader): # 枚举出来 if not args.no_cuda: # 数据处理是否用gpu images = images.cuda() labels = labels.cuda()