def forward(datas_train,is_training): if FLAGS.typenets == 'vggnet16': net = vggnet(datas_train) net = slim.fully_connected(net,num_classes,activation_fn=None,scope='fc1') #the last layer outputs = slim.softmax(net, scope='predictions') #outputs = tf.nn.softmax(net,namescope='output') elif FLAGS.typenets == 'resnet50': with slim.arg_scope(resnet_v2.resnet_arg_scope(is_training=is_training)): net,_ = resnet_v2.resnet_v2_50(datas_train,num_classes=num_classes) net = slim.flatten(net,scope='flat2') outputs = slim.softmax(net, scope='predictions') elif FLAGS.typenets == 'resnet101': with slim.arg_scope(resnet_v2.resnet_arg_scope(is_training=is_training)): net,_ = resnet_v2.resnet_v2_101(datas_train,num_classes=num_classes) net = slim.flatten(net,scope='flat2') outputs = slim.softmax(net, scope='predictions') elif FLAGS.typenets == 'resnet152': with slim.arg_scope(resnet_v2.resnet_arg_scope(is_training=is_training)): net,_ = resnet_v2.resnet_v2_152(datas_train,num_classes=num_classes) net = slim.flatten(net,scope='flat2') outputs = slim.softmax(net, scope='predictions') elif FLAGS.typenets == 'resnet200': with slim.arg_scope(resnet_v2.resnet_arg_scope(is_training=is_training)): net,_ = resnet_v2.resnet_v2_200(datas_train,num_classes=num_classes) net = slim.flatten(net,scope='flat2') outputs = slim.softmax(net, scope='predictions') elif FLAGS.typenets == 'simple': outputs = forward_simple(datas_train,is_training) return outputs
def build_frontend(inputs, frontend, is_training=True, pretrained_dir="models"): if frontend == 'ResNet50': with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, end_points = resnet_v2.resnet_v2_50( inputs, is_training=is_training, scope='resnet_v2_50') frontend_scope = 'resnet_v2_50' init_fn = slim.assign_from_checkpoint_fn( model_path=os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'), var_list=slim.get_model_variables('resnet_v2_50'), ignore_missing_vars=True) elif frontend == 'ResNet101': with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, end_points = resnet_v2.resnet_v2_101( inputs, is_training=is_training, scope='resnet_v2_101') frontend_scope = 'resnet_v2_101' init_fn = slim.assign_from_checkpoint_fn( model_path=os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'), var_list=slim.get_model_variables('resnet_v2_101'), ignore_missing_vars=True) elif frontend == 'ResNet152': with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, end_points = resnet_v2.resnet_v2_152( inputs, is_training=is_training, scope='resnet_v2_152') frontend_scope = 'resnet_v2_152' init_fn = slim.assign_from_checkpoint_fn( model_path=os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'), var_list=slim.get_model_variables('resnet_v2_152'), ignore_missing_vars=True) elif frontend == 'MobileNetV2': with slim.arg_scope(mobilenet_v2.training_scope()): logits, end_points = mobilenet_v2.mobilenet( inputs, is_training=is_training, scope='mobilenet_v2', base_only=True) frontend_scope = 'mobilenet_v2' init_fn = slim.assign_from_checkpoint_fn( model_path=os.path.join(pretrained_dir, 'mobilenet_v2_1.4_224.ckpt'), var_list=slim.get_model_variables('mobilenet_v2'), ignore_missing_vars=True) elif frontend == 'InceptionV4': with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits, end_points = inception_v4.inception_v4( inputs, is_training=is_training, scope='inception_v4') frontend_scope = 'inception_v4' init_fn = slim.assign_from_checkpoint_fn( model_path=os.path.join(pretrained_dir, 'inception_v4.ckpt'), var_list=slim.get_model_variables('inception_v4'), ignore_missing_vars=True) else: raise ValueError( "Unsupported fronetnd model '%s'. This function only supports ResNet50, ResNet101, ResNet152, and MobileNetV2" % (frontend)) return logits, end_points, frontend_scope, init_fn
def build_deeplabv3_plus(inputs, num_classes, preset_model='DeepLabV3+-Res50', weight_decay=1e-5, is_training=True, pretrained_dir="models"): """ Builds the DeepLabV3 model. Arguments: inputs: The input tensor= preset_model: Which model you want to use. Select which ResNet model to use for feature extraction num_classes: Number of classes Returns: DeepLabV3 model """ if preset_model == 'DeepLabV3_plus-Res50': with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)): logits, end_points = resnet_v2.resnet_v2_50(inputs, is_training=is_training, scope='resnet_v2_50') resnet_scope='resnet_v2_50' # DeepLabV3 requires pre-trained ResNet weights init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'), slim.get_model_variables('resnet_v2_50')) elif preset_model == 'DeepLabV3_plus-Res101': with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)): logits, end_points = resnet_v2.resnet_v2_101(inputs, is_training=is_training, scope='resnet_v2_101') resnet_scope='resnet_v2_101' # DeepLabV3 requires pre-trained ResNet weights init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'), slim.get_model_variables('resnet_v2_101')) elif preset_model == 'DeepLabV3_plus-Res152': with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)): logits, end_points = resnet_v2.resnet_v2_152(inputs, is_training=is_training, scope='resnet_v2_152') resnet_scope='resnet_v2_152' # DeepLabV3 requires pre-trained ResNet weights init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'), slim.get_model_variables('resnet_v2_152')) else: raise ValueError("Unsupported ResNet model '%s'. This function only supports ResNet 50, ResNet 101, and ResNet 152" % (preset_model)) label_size = tf.shape(inputs)[1:3] encoder_features = end_points['pool2'] net = AtrousSpatialPyramidPoolingModule(end_points['pool4']) net = slim.conv2d(net, 256, [1, 1], scope="conv_1x1_output", activation_fn=None) decoder_features = Upsampling(net, label_size / 4) encoder_features = slim.conv2d(encoder_features, 48, [1, 1], activation_fn=tf.nn.relu, normalizer_fn=None) net = tf.concat((encoder_features, decoder_features), axis=3) net = slim.conv2d(net, 256, [3, 3], activation_fn=tf.nn.relu, normalizer_fn=None) net = slim.conv2d(net, 256, [3, 3], activation_fn=tf.nn.relu, normalizer_fn=None) net = Upsampling(net, label_size) net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, scope='logits') return net, init_fn
def __init__(self, height, width, channel): self.height = height self.width = width self.channel = channel print('Loading Saliency graph...') self.graph = tf.Graph() with self.graph.as_default(): self.inputs = tf.placeholder( shape=[None, self.height, self.width, self.channel], name=constant.FRAME_FEATURE_INPUTS, dtype=tf.float32) self.neuron_selector = tf.placeholder(tf.int32) with slim.arg_scope(resnet.resnet_arg_scope()): self.prediction, _ = resnet.resnet_v2_152(inputs=self.inputs, num_classes=1001, global_pool=True, is_training=False) self.y = self.prediction[0][self.neuron_selector] self.pred = tf.argmax(self.prediction, axis=1) print('Resnet 152 model loaded.') self.saver = tf.train.Saver() self.saliency_sess = tf.Session(graph=self.graph) self.saver.restore(self.saliency_sess, constant.PRETRAINED_ROOT + constant.RESNET_152_CKPT) print( 'Resnet 152 checkpoints restored.\nSaliency graph load completed!\n' )
def forward_tran_advers(x_img=None, label_index=None, number_of_classes=5, layer_name=None, Training=True): # 根据logits,label_index,计算目标函数对conv层maps的梯度 with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, end_points = resnet_v2.resnet_v2_152_adversarial(inputs=x_img,\ num_classes=number_of_classes,\ is_training=Training, reuse=tf.AUTO_REUSE) prob = tf.nn.softmax(logits) if Training == True: prob_max_label = label_index else: prob_max_label = tf.argmax(prob, 1) label_hot = tf.one_hot(prob_max_label, number_of_classes) cost = (-1) * tf.reduce_sum(tf.multiply(tf.log(prob), label_hot), axis=1) y_c = tf.reduce_sum(tf.multiply(logits, label_hot), axis=1) # Grad CAM #target_conv_layer=end_points[layer_name] target_conv_layer = x_img #gb_grad = tf.gradients(cost, x_img)[0] # guided Grad-CAM target_grad_ac = tf.gradients(y_c, target_conv_layer)[0] # Grad CAM target_grad_yc = tf.gradients(tf.exp(y_c), target_conv_layer)[0] # Grad CAM ++ return target_conv_layer, target_grad_ac, target_grad_yc, logits, end_points
def arch_resnet_v2_50(self, X, num_classes, dropout_keep_prob=0.8, is_train=False): arg_scope = resnet_arg_scope() with slim.arg_scope(arg_scope): net_vis, end_points = resnet_v2_50(X, is_training=is_train) with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d], stride=1, padding='SAME'): with tf.variable_scope('Logits_out'): net = slim.conv2d(net_vis, 1000, [1, 1], activation_fn=None, normalizer_fn=None, scope='Logits_out0') net = slim.dropout(net, dropout_keep_prob, scope='Dropout_1b_out0') net = slim.conv2d(net, 200, [1, 1], activation_fn=None, normalizer_fn=None, scope='Logits_out1') net = slim.dropout(net, dropout_keep_prob, scope='Dropout_1b_out1') net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, normalizer_fn=None, scope='Logits_out2') net = tf.squeeze(net, [1, 2], name='SpatialSqueeze') return net, net_vis
def arch_resnet_v2_50_rnn_attention(self, X, num_classes, dropout_keep_prob=0.8, is_train=False): rnn_size = 256 num_layers = 2 attention_size = 64 arg_scope = resnet_arg_scope() with slim.arg_scope(arg_scope): net_vis, end_points = resnet_v2_50(X, is_training=is_train) with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d], stride=1, padding='SAME'): with tf.variable_scope('Logits_out'): orig_shape = net_vis.get_shape().as_list() net = tf.reshape(net_vis, [-1, orig_shape[1] * orig_shape[2], orig_shape[3]]) def gru_cell(): return tf.contrib.rnn.GRUCell(run_size) def lstm_cell(): return tf.contrib.rnn.LSTMCell(rnn_size) def attn_cell(): return tf.contrib.rnn.DropoutWrapper(lstm_cell(), output_keep_prob=dropout_keep_prob) stack = tf.contrib.rnn.MultiRNNCell([lstm_cell() for _ in range(0, num_layers)], state_is_tuple=True) net, _ = tf.nn.dynamic_rnn(stack, net, dtype=tf.float32) net = tf.transpose(net, (1, 0, 2)) net = attention(net, attention_size, True) # #net = slim.fully_connected(net[-1], 256, activation_fn=tf.nn.relu, scope='Logits_out0') net = slim.fully_connected(net, num_classes, activation_fn=None,scope='Logits_out1') return net, net_vis
def arch_resnet_v2_50(self, X, num_classes, dropout_keep_prob=0.8, is_train=False, embedding_size=128): arg_scope = resnet_arg_scope() with slim.arg_scope(arg_scope): net_vis, end_points = resnet_v2_50(X, is_training=is_train) with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d], stride=1, padding='SAME'): with tf.variable_scope('Logits_out'): # 8 x 8 x 1536 net_vis = slim.avg_pool2d(net_vis, net_vis.get_shape()[1:3], padding='VALID', scope='AvgPool_1a_out') # 1 x 1 x 1536 net_vis = slim.dropout(net_vis, dropout_keep_prob, scope='Dropout_1b_out') net_vis = slim.flatten(net_vis, scope='PreLogitsFlatten_out') # 1536 net_vis = slim.fully_connected(net_vis, embedding_size, activation_fn=tf.nn.relu, scope='Logits_out0') net = slim.fully_connected(net_vis, num_classes, activation_fn=None, scope='Logits_out1') return net, net_vis
def evalidate(flag, args): with tf.Graph().as_default(): queue_loader = data_loader(False, batch_size=args.bsize, num_epochs=args.ep, dataset_dir=args.dataset_dir) with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, end_points = resnet_v2.resnet_v2_101(queue_loader.images, num_classes=764, is_training=flag) names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({ "accuracy": slim.metrics.accuracy(np.argmax(logits, 1), queue_loader.labels) }) for name, value in names_to_values.items(): op_name = 'eval_{}'.format(name) op = tf.summary.scalar(op_name, value) tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) slim.evaluation.evaluate_once( master='', checkpoint_path=args.modelpath, logdir=args.evallog, num_evals=queue_loader.num_batches, eval_op=list(names_to_updates.values()), variables_to_restore=slim.get_variables_to_restore())
def inference(X_tensor, number_of_classes, is_training_placeholder): with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, end_points = resnet_v2.resnet_v2_152(inputs=X_tensor,\ num_classes=number_of_classes,\ is_training=is_training_placeholder,\ reuse=tf.AUTO_REUSE) return logits, end_points
def resnet_process_data_dir(work_dir, data_dir, model_dir): resNet_path = os.path.join(work_dir, 'ResNet') pickle_out = os.path.join(resNet_path, 'ResNet_preprocess.pk') if not os.path.exists(resNet_path): os.makedirs(resNet_path) if os.path.isfile(pickle_out): os.remove(pickle_out) print('ResNet preprocessing for ' + resNet_path) # Image directory info. img_files = sorted( [name for name in os.listdir(data_dir) if _is_img(name)]) print('data_dir:{}'.format(data_dir)) print('frames_num:{}'.format(len(img_files))) print('img_files:') print(img_files) img_list = [] for pic in img_files: img_list.append(os.path.join(data_dir, pic)) # Pre-process using ResNet. img_size = resnet_v2.resnet_v2.default_image_size resnet_v2_model = os.path.join(model_dir, 'resnet_v2_50.ckpt') print('resnet_v2_model:{}'.format(resnet_v2_model)) with tf.Graph().as_default(): processed_images = [] for i, img in enumerate(img_list): # 读取图片并按照jpg格式转化为3通道的张量 image = tf.image.decode_jpeg(tf.read_file(img), channels=3) # 预处理:双线性插值resize固定尺寸(224),中心裁剪0.875,float类型并且[0,1],normalization去均值化除以标准差 processed_images.append( inception_preprocessing.preprocess_image(image, img_size, img_size, is_training=False)) processed_images = tf.convert_to_tensor(processed_images) with slim.arg_scope(resnet_v2.resnet_arg_scope()): # Return ResNet 2048 vector. logits, _ = resnet_v2.resnet_v2_50(processed_images, num_classes=None, is_training=False) init_fn = slim.assign_from_checkpoint_fn( resnet_v2_model, slim.get_variables_to_restore()) with tf.Session() as sess: init_fn(sess) np_images, resnet_vectors = sess.run([processed_images, logits]) resnet_vectors = resnet_vectors[:, 0, 0, :] print('form pickle_data......') # Save preprocessed data to pickle file. pickle_data = {'frame_resnet_vectors': resnet_vectors} pickle.dump(pickle_data, open(pickle_out, 'wb')) print('resNet finished!') print('{} has been dumped over!'.format(pickle_out))
def get_resnet_param(layer): arg_scope = resnet_arg_scope() if layer == 50: pretrained_model = resnet_v2_50 elif layer == 101: pretrained_model = resnet_v2_101 elif layer == 152: pretrained_model = resnet_v2_152 else: raise Exception('error model %d' % layer) ckpt_path = 'pre-trained/tensorflow-resnet-pretrained/resnet_v2_%d.ckpt' % layer return arg_scope, pretrained_model, ckpt_path
def _build(self): reuse = True if self.built else None with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, end_points = resnet_v2.resnet_v2_101( self.input, num_classes=self.num_classes, is_training=False, reuse=reuse) self.built = True self.end_points = end_points self.logits = logits if not self.ckpt_loaded: saver = tf.train.Saver() saver.restore(self.sess, ckpt_dir + 'resnet_v2_101.ckpt') self.ckpt_loaded = True
def encoder_resnet101(input_x=None, n_classes=20, is_training=True): with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=1e-5)): logits, end_points = resnet_v2.resnet_v2_101(input_x, is_training=is_training, scope='resnet_v2_101') x = tf.keras.layers.GlobalAveragePooling2D()(logits) x = tf.keras.layers.Dense(n_classes, activation=None, name='predictions_no_softmax')(x) ''' same on tensorflow pure # Global average pooling x = tf.reduce_mean(x6_, [1,2]) # Last layer x = tf.layers.dense(x, n_classes) ''' return x
def main(_): print 'reading npy...' data = np.load('../data/1st.npy') jpg_list=[] for i in range(len(data)): jpg_list.append(str(i)+'.jpg') train_order = np.load('../data/train.npy') validation_order = np.load('../data/validation.npy') one_epoch_iter = len(train_order)/FLAGS.batch_size print 'reading finished' sess = tf.Session() arg_scope = resnet_v2.resnet_arg_scope() print 'building network...' with slim.arg_scope(arg_scope): hg = resnet.resnet(is_training=True) global_step = tf.Variable(0,name='global_step',trainable=False) learning_rate = tf.train.exponential_decay(FLAGS.learning_rate,global_step,0.5*FLAGS.max_epoch*len(train_order)/FLAGS.batch_size,0.1,staircase=True) tf.summary.scalar('learning_rate', learning_rate) optimizer = tf.train.AdamOptimizer(learning_rate) init_fn = _get_init_fn() variables_to_train = _get_variables_to_train() #print variables_to_train #return update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_op = optimizer.minimize(hg.total_loss,global_step=global_step,var_list=variables_to_train) merged_summary = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.summary_dir,sess.graph) sess.run(tf.initialize_all_variables()) saver = tf.train.Saver(max_to_keep=3) init_fn(sess) print 'building finished' def train_step(input_image,input_label): feed_dict={} feed_dict[hg.input_image]=input_image feed_dict[hg.input_label]=input_label temp,step,ce_loss,l2_loss,total_loss,summary,output= sess.run([train_op,global_step,hg.ce_loss,hg.l2_loss,hg.total_loss,merged_summary,hg.output],feed_dict) summary_writer.add_summary(summary,step) return output,ce_loss,l2_loss,total_loss def validation_step(current_step): print 'Validating...' all_ce_loss = 0 all_l2_loss = 0 all_total_loss = 0 all_output = [] all_label = [] for i in range(int(len(validation_order)/18)): input_image = get_data.get_jpg_test(jpg_list,validation_order[18*i:18*(i+1)]) input_label = get_data.get_label(data,validation_order[18*i:18*(i+1)]) feed_dict={} feed_dict[hg.input_image]=input_image feed_dict[hg.input_label]=input_label ce_loss,l2_loss,total_loss,output= sess.run([hg.ce_loss,hg.l2_loss,hg.total_loss,hg.output],feed_dict) all_ce_loss += ce_loss all_l2_loss += l2_loss all_total_loss += total_loss for i in output: all_output.append(i) for i in input_label: all_label.append(i) all_output = np.array(all_output) all_label = np.array(all_label) auc = roc_auc_score(all_label,all_output) ce_loss = all_ce_loss/283.0 l2_loss = all_l2_loss/283.0 total_loss = all_total_loss/283.0 all_output=np.reshape(all_output,(-1)) all_label=np.reshape(all_label,(-1)) ap = average_precision_score(all_label,all_output) time_str = datetime.datetime.now().isoformat() tempstr = "{}: auc {:g}, ap {:g}, ce_loss {:g}, l2_loss {:g}, total_loss {:g}".format(time_str, auc, ap, ce_loss, l2_loss, total_loss) print(tempstr) summary_writer.add_summary(MakeSummary('validation/auc',auc),current_step) summary_writer.add_summary(MakeSummary('validation/ap',ap),current_step) summary_writer.add_summary(MakeSummary('validation/ce_loss',ce_loss),current_step) return ce_loss best_ce_loss = 10000 best_iter = 0 smooth_ce_loss = 0 smooth_l2_loss = 0 smooth_total_loss = 0 temp_label=[] temp_output=[] for one_epoch in range(FLAGS.max_epoch): print('epoch '+str(one_epoch+1)+' starts!') np.random.shuffle(train_order) for i in range(int(len(train_order)/float(FLAGS.batch_size))): start = i*FLAGS.batch_size end = (i+1)*FLAGS.batch_size input_image = get_data.get_jpg_train(jpg_list,train_order[start:end]) input_label = get_data.get_label(data,train_order[start:end]) output,ce_loss,l2_loss,total_loss = train_step(input_image,input_label) smooth_ce_loss+=ce_loss smooth_l2_loss+=l2_loss smooth_total_loss+=total_loss temp_label.append(input_label) temp_output.append(output) current_step = tf.train.global_step(sess,global_step) if current_step%10==0: ce_loss=smooth_ce_loss/10.0 l2_loss=smooth_l2_loss/10.0 total_loss=smooth_total_loss/10.0 temp_output = np.reshape(np.array(temp_output),(-1)) temp_label = np.reshape(np.array(temp_label),(-1)) ap = average_precision_score(temp_label,temp_output) temp_output = np.reshape(temp_output,(-1,100)) temp_label = np.reshape(temp_label,(-1,100)) try: auc = roc_auc_score(temp_label,temp_output) except ValueError: print 'ytrue error for auc' else: time_str = datetime.datetime.now().isoformat() tempstr = "{}: step {}, auc {:g}, ap {:g}, ce_loss {:g}, l2_loss {:g}, total_loss {:g}".format(time_str, current_step, auc, ap,ce_loss, l2_loss, total_loss) print(tempstr) summary_writer.add_summary(MakeSummary('train/auc',auc),current_step) summary_writer.add_summary(MakeSummary('train/ap',ap),current_step) temp_output=[] temp_label=[] smooth_ce_loss = 0 smooth_l2_loss = 0 smooth_total_loss = 0 if current_step%int(one_epoch_iter*FLAGS.save_epoch)==0: ce_loss = validation_step(current_step) if ce_loss<best_ce_loss: print 'currently the validation ce_loss is less the previous best one!!!' best_ce_loss=ce_loss best_iter=current_step print 'saving model' path = saver.save(sess,FLAGS.model_dir+'resnet_model',global_step=current_step) print 'have saved model to '+path print 'warmup training has been finished !' print 'the best model iter is '+str(best_iter) print 'the best ce_loss on validation is '+str(best_ce_loss)
def test_resnet(device, num_classes, num_layers, dataset, normalization, checkpoint): """ Computes accuracy for the test dataset Inputs: device - gpu device num_classes - number of output classes num_layers - number of layers selected for ResNet dataset - dataset selected, options are val and test normalization - normalization used options are standard z-score normalization or unet normalization checkpoint - file where graph model weights are stored Output: None """ print dataset os.environ['CUDA_VISIBLE_DEVICES'] = str(device) # use nvidia-smi to see available options '0' means first gpu config = GleasonConfig() # loads pathology configuration if dataset == 'val': print "Using val..." config.test_fn = os.path.join(config.main_dir, 'tfrecords/val.tfrecords') elif dataset == 'test': print "Using test..." config.test_fn = os.path.join(config.main_dir, 'tfrecords/test.tfrecords') else: config.test_fn = None config.test_checkpoint = checkpoint print "Loading checkpoint: {0}".format(checkpoint) if int(num_classes) == 2: print "Converting to Output Shape to Binary..." config.output_shape = 2 elif int(num_classes) == 4: config.output_shape = 4 else: raise Exception('Invalid number of classes!') batch_size = 128 # loading test data test_meta = np.load(tfrecord2metafilename(config.test_fn)) print 'Using {0} tfrecords: {1} | {2} images'.format(dataset, config.test_fn, len(test_meta['labels'])) test_filename_queue = tf.train.string_input_producer([config.test_fn] , num_epochs=1) # 1 epoch, passing through the # the dataset once test_img, test_t_l, test_f_p = read_and_decode(filename_queue = test_filename_queue, img_dims = config.input_image_size, model_dims = config.model_image_size, size_of_batch = batch_size, augmentations_dic = config.val_augmentations_dic, num_of_threads = 4, shuffle = False) if int(num_classes) == 2: print "Converting labels to Binary..." test_t_l = tf.clip_by_value(test_t_l, 0, 1) if num_layers == "50": print "Loading Resnet 50..." if normalization == "standard": print "Using standard normalization..." test_img = normalize(test_img) elif normalization == "unet": print "Using unet normalization..." test_img,_ = unet_preprocess.unet(test_img, is_training = False, is_batch_norm = False, num_channels = 1) else: raise Exception('Not known normalization! Options are: standard and unet.') with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay = config.l2_reg)): test_target_logits, _ = resnet_v2.resnet_v2_50(inputs = test_img, num_classes = config.output_shape, is_training = False) elif num_layers == "101": print "Loading Resnet 101..." if normalization == "standard": print "Using standard normalization..." test_img = normalize(test_img) elif normalization == "unet": print "Using unet normalization..." test_img,_ = unet_preprocess.unet(test_img, is_training = False, is_batch_norm = False, num_channels = 1) else: raise Exception('Not known normalization! Options are: standard and unet.') with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay = config.l2_reg)): test_target_logits, _ = resnet_v2.resnet_v2_101(inputs = test_img, num_classes = config.output_shape, is_training = False) else: raise Expection("Wrong number of layers! allowed numbers are 50 and 101.") target_prob = tf.nn.softmax(test_target_logits) prob_and_label_files = [target_prob, test_t_l, test_f_p] restorer = tf.train.Saver() print "Variables stored in checkpoint:" print_tensors_in_checkpoint_file(file_name=config.test_checkpoint, tensor_name='', all_tensors='') with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess: sess.run(tf.group(tf.global_variables_initializer(),tf.local_variables_initializer())) restorer.restore(sess, config.test_checkpoint) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) all_predictions_target = [] all_predictions_t_n = [] all_labels = [] all_files = [] batch_num = 1 try: print "Total number of batch iterations needed: {0}".format(int(math.ceil(len(test_meta['labels'])/batch_size))) while not coord.should_stop(): np_prob_and_label_files = sess.run(prob_and_label_files) target_probs = np_prob_and_label_files[0] labels = np_prob_and_label_files[1] files = np_prob_and_label_files[2] all_labels += list(labels) all_files += list(files) all_predictions_target += list(np.argmax(target_probs, axis=1)) print "evaluating current batch number: {0}".format(batch_num) batch_num +=1 except tf.errors.OutOfRangeError: print "{0} accuracy: {1:.2f}".format(dataset, (metrics.accuracy_score(all_labels, all_predictions_target)*100)) if int(num_classes) == 2: print "{0} precision: {1:.2f}".format(dataset, (metrics.precision_score(all_labels, all_predictions_target)*100)) print "{0} recall: {1:.2f}".format(dataset, (metrics.recall_score(all_labels, all_predictions_target)*100)) print finally: coord.request_stop() coord.join(threads)
def main(_): print 'reading npy...' #trainlist, labels = read_csv.train_data() #jpg_list = np.load('../jpg_1st.npy') data = np.load('../1st.npy') jpg_list = [] for i in range(len(data)): jpg_list.append(str(i) + '.jpg') #train_order = np.load('../train.npy') #test_order = np.load('../test.npy') test_order = [] for i in range(len(data)): test_order.append(i) sess = tf.Session() arg_scope = resnet_v2.resnet_arg_scope() print 'building network...' with slim.arg_scope(arg_scope): hg = resnet_test.resnet(is_training=False) init_fn = _get_init_fn() merged_summary = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.summary_dir, sess.graph) sess.run(tf.initialize_all_variables()) saver = tf.train.Saver(max_to_keep=None) init_fn(sess) print 'building finished' def test_step(): print 'testing...' # all_ce_loss = 0 # all_l2_loss = 0 # all_total_loss = 0 # all_output = [] # all_label = [] all_feature = [] batch_size = 17 * 3 for i in range(int(len(test_order) / batch_size)): input_image = get_data.get_jpg_test( jpg_list, test_order[batch_size * i:batch_size * (i + 1)]) input_label = get_data.get_label( data, test_order[batch_size * i:batch_size * (i + 1)]) feed_dict = {} feed_dict[hg.input_image] = input_image feature = sess.run(hg.feature, feed_dict) for i in feature: all_feature.append(i) # for i in input_label: # all_label.append(i) # all_output = np.array(all_output) # all_label = np.array(all_label) # #average_precision = average_precision_score(all_label,all_output) # np.save('output.npy',all_output) # np.save('label.npy',all_label) # auc = roc_auc_score(all_label,all_output) # loglike = log_likelihood(all_label,all_output) # time_str = datetime.datetime.now().isoformat() # tempstr = "{}: auc {:g}, log_likelihood {:g}".format(time_str, auc, loglike) # print(tempstr) # all_output=np.reshape(all_output,(-1)) # all_label=np.reshape(all_label,(-1)) # ap = average_precision_score(all_label,all_output) # auc_2 = roc_auc_score(all_label,all_output) # print 'ap:'+str(ap) # print 'auc_2:'+str(auc_2) np.save('resnet50_feature.npy', all_feature) test_step()
batch_size=batch_size, num_classes=num_classes, shuffle=False) # create an reinitializable iterator given the dataset structure iterator_test = Iterator.from_structure(test_data.data.output_types, test_data.data.output_shapes) images_batch_test, labels_batch_test = iterator_test.get_next() # Ops for initializing the two different iterators test_init_op = iterator_test.make_initializer(test_data.data) images_batch = tf.concat([images_batch_test, images_batch_test], axis=0) y = tf.concat([labels_batch_test, labels_batch_test], axis=0) with slim.arg_scope(resnet.resnet_arg_scope()): Gap5, CONV5, net, _ = resnet.resnet_v2_50(images_batch, is_training=False) net = tf.nn.dropout(net, 1.0) net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, normalizer_fn=None, scope='logits') score = tf.squeeze(net, [1, 2], name='SpatialSqueeze') SCORE = tf.reshape(score, [2 * batch_size, -1]) # Evaluation op: Accuracy of the model with tf.name_scope("accuracy"): correct_pred = tf.equal( tf.argmax(tf.slice(SCORE, [0, 0], [batch_size, num_classes]), 1), tf.argmax(tf.slice(y, [0, 0], [batch_size, num_classes]), 1))
def train(retain_flag=True, start_step=0): print('train(retain_flag=%s, start_step=%s)' % (retain_flag, start_step)) with tf.Graph().as_default(): # define placeholder train_mode = tf.placeholder(tf.bool, name='train_mode') input_batch = tf.placeholder(tf.float32, [None, IMAGE_HEIGHT, IMAGE_WIDTH, 3], 'input_batch') # define model resnet_reid = ResnetReid(train_flags.dropout) input_batch = tf.cond( train_mode, lambda: resnet_reid.process_image( input_batch, train_flags.train_batch_size), lambda: input_batch, 'process_image') with slim.arg_scope(resnet_arg_scope()): # input_batch: [batch, height, width, 3] values scaled [0.0, 1.0], dtype = tf.float32 resnet_avg_pool, end_points = resnet_v2_50(input_batch, is_training=False, global_pool=True) # Define the scopes that you want to exclude for restoration variables_to_restore = slim.get_variables_to_restore( exclude=['beta2_power']) resnet_reid.build(resnet_avg_pool, train_mode) # define loss loss = tf.cond(train_mode, lambda: _model_loss(resnet_reid), lambda: tf.constant([0.0], dtype=tf.float32), 'chose_loss') # Create an optimizer that performs gradient descent. global_step = get_or_create_global_step() lr = tf.train.exponential_decay(train_flags.initial_learning_rate, global_step, train_flags.decay_steps, train_flags.decay_rate, staircase=True) vars_to_optimize = [v for v in tf.trainable_variables()] # vars_to_optimize = [v for v in tf.trainable_variables() if ('add' in v.name)] print('\nvariables to optimize') for v in vars_to_optimize: print(v.name, v.get_shape().as_list()) tf.summary.histogram(v.name, v) opt = tf.train.AdamOptimizer(lr) grads = opt.compute_gradients(loss, var_list=vars_to_optimize) apply_gradient_op = opt.apply_gradients(grads, global_step=global_step) train_op = tf.group(apply_gradient_op) # define sess sess = tf.Session() sess.run(tf.global_variables_initializer()) # define summary and saver summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(train_flags.output_summary_path, graph=sess.graph) saver = tf.train.Saver(max_to_keep=8) # retrain or continue if retain_flag: saver_resnet = tf.train.Saver(variables_to_restore) saver_resnet.restore(sess, train_flags.resnet_checkpoint_file) else: # load checkpoint ckpt = tf.train.get_checkpoint_state( train_flags.output_check_point_path) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) print('load checkpoint') print('start training') for step in range(start_step, train_flags.max_step): # input image batch = resnet_reid.get_train_image_batch_direct( train_flags.id_path_train_path, train_flags.return_id_num, train_flags.image_num_every_id) # if step == 0: # load mode # change_file_flag = True # else: # change_file_flag = False # batch = resnet_reid.get_train_image_batch(train_flags.id_image_path, train_flags.id_image_train_num, # train_flags.return_id_num, train_flags.image_num_every_id, # change_file=change_file_flag) # start run start_time = time.time() _, loss_value = sess.run([train_op, loss], feed_dict={ input_batch: batch, train_mode: True }) # _, loss_value, f = sess.run([train_op, loss, resnet_reid.output], # feed_dict={input_batch: batch, train_mode: True}) # print('feature abs mean: %s' % (np.mean(np.abs(f)))) duration = time.time() - start_time assert not np.isnan(loss_value), 'Model diverged with loss = NaN' if step % 10 == 0: examples_per_sec = train_flags.train_batch_size / float( duration) format_str = '%s: step %d, loss = %.2f (%.1f examples/sec; %.3f sec/batch)' print(format_str % (datetime.now(), step, loss_value, examples_per_sec, duration)) if step % 100 == 0: summary_str, feature = sess.run( [summary_op, resnet_reid.output], feed_dict={ input_batch: batch, train_mode: True }) print('feature abs mean: %s' % (np.mean(np.abs(feature)))) summary_str = sess.run(summary_op, feed_dict={ input_batch: batch, train_mode: True }) summary_writer.add_summary(summary_str, step) if step % 5000 == 0 or (step + 1) == train_flags.max_step: # Save the model checkpoint periodically. checkpoint_path = os.path.join( train_flags.output_check_point_path, train_flags.checkpoint_name) saver.save(sess, checkpoint_path, global_step=step) # do test: send every image in valid_gallery.csv and valid_probe.csv, then get feature vector # save all feature vector in npy def valid(gallery_flag): # get feature batch features_num = train_flags.valid_gallery_num if gallery_flag else train_flags.valid_probe_num features = np.zeros( (features_num, train_flags.output_feature_dim), dtype=float) batch_len = train_flags.test_batch_size batch_name = 'valid_gallery_batch_index: ' if gallery_flag else 'valid_probe_batch_index: ' end_len = features_num % batch_len for batch_index in range(0, features_num - end_len, batch_len): batch = resnet_reid.get_test_image_batch( batch_index, batch_len, train_flags.id_image_path, 'valid', gallery_flag) batch_feature = sess.run(resnet_reid.output, feed_dict={ input_batch: batch, train_mode: False }) features[batch_index:batch_index + batch_len, :] = batch_feature print(batch_name + str(batch_index) + '-' + str(batch_index + batch_len - 1)) if end_len != 0: batch_index += batch_len batch = resnet_reid.get_test_image_batch( batch_index, batch_len, train_flags.id_image_path, 'valid', gallery_flag) batch_feature = sess.run(resnet_reid.output, feed_dict={ input_batch: batch, train_mode: False }) features[batch_index:batch_index + end_len, :] = batch_feature[:end_len] print(batch_name + str(batch_index) + '-' + str(batch_index + end_len - 1)) # save feature features_npy_name = 'valid_gallery_features_step-%d.npy' if gallery_flag else 'valid_probe_features_step-%d.npy' features_npy_path = os.path.join( train_flags.output_test_features_path, features_npy_name % step) np.save(features_npy_path, features) valid(False) valid(True) sess.close()
def generate_features(predict_flag, gallery_flag): print('generate_features(%s, %s)' % (predict_flag, gallery_flag)) with tf.Graph().as_default(): # define placeholder train_mode = tf.placeholder(tf.bool, name='train_mode') input_batch = tf.placeholder(tf.float32, [None, IMAGE_HEIGHT, IMAGE_WIDTH, 3], 'input_batch') # define model resnet_reid = ResnetReid() with slim.arg_scope(resnet_arg_scope()): # input_batch: [batch, height, width, 3] values scaled [0.0, 1.0], dtype = tf.float32 resnet_avg_pool, end_points = resnet_v2_50(input_batch, is_training=False, global_pool=True) resnet_reid.build(resnet_avg_pool, train_mode) # define sess with tf.Session() as sess: sess.run(tf.global_variables_initializer()) # load checkpoint saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state( train_flags.output_check_point_path) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) print('load checkpoint') # define parameter if gallery_flag and predict_flag: features_num = train_flags.predict_gallery_num features_npy_name = 'predict_gallery_features.npy' get_image_batch = resnet_reid.get_predict_image_batch file_title = 'predict' elif gallery_flag and (not predict_flag): features_num = train_flags.train_200_gallery_num features_npy_name = 'train_200_gallery_features.npy' get_image_batch = resnet_reid.get_test_image_batch file_title = 'train_200' elif (not gallery_flag) and predict_flag: features_num = train_flags.predict_probe_num features_npy_name = 'predict_probe_features.npy' get_image_batch = resnet_reid.get_predict_image_batch file_title = 'predict' else: features_num = train_flags.train_200_probe_num features_npy_name = 'train_200_probe_features.npy' get_image_batch = resnet_reid.get_test_image_batch file_title = 'train_200' print('start generate features') # get feature batch features = np.zeros((features_num, train_flags.output_feature_dim), dtype=float) batch_len = train_flags.test_batch_size end_len = features_num % batch_len for batch_index in range(0, features_num - end_len, batch_len): batch = get_image_batch(batch_index, batch_len, train_flags.id_image_path, file_title, gallery_flag) batch_feature = sess.run(resnet_reid.output, feed_dict={ input_batch: batch, train_mode: False }) features[batch_index:batch_index + batch_len, :] = batch_feature print('batch_index: ' + str(batch_index) + '-' + str(batch_index + batch_len - 1)) if end_len != 0: batch_index += batch_len batch = get_image_batch(batch_index, batch_len, train_flags.id_image_path, file_title, gallery_flag) batch_feature = sess.run(resnet_reid.output, feed_dict={ input_batch: batch, train_mode: False }) features[batch_index:batch_index + end_len, :] = batch_feature[:end_len] print('batch_index: ' + str(batch_index) + '-' + str(batch_index + end_len - 1)) # save feature features_npy_path = os.path.join( train_flags.output_test_features_path, features_npy_name) np.save(features_npy_path, features)
def encoder(self, x_ph, reuse=False, trainable=True): with tf.variable_scope(self.name): x_reshape = tf.tile(x_ph, [1,1,1,3]) with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, _ = resnet_v2.resnet_v2_101(x_reshape, 1001, is_training=True) return logits
def build_deeplabv3(inputs, num_classes, preset_model='DeepLabV3-Res50', upscaling_method="bilinear", weight_decay=1e-5, is_training=True, pretrained_dir="models"): """ Builds the DeepLabV3 model. Arguments: inputs: The input tensor= preset_model: Which model you want to use. Select which ResNet model to use for feature extraction num_classes: Number of classes Returns: DeepLabV3 model """ inputs = mean_image_subtraction(inputs) if preset_model == 'DeepLabV3-Res50': with slim.arg_scope( resnet_v2.resnet_arg_scope(weight_decay=weight_decay)): logits, end_points = resnet_v2.resnet_v2_50( inputs, is_training=is_training, scope='resnet_v2_50') resnet_scope = 'resnet_v2_50' # DeepLabV3 requires pre-trained ResNet weights init_fn = slim.assign_from_checkpoint_fn( os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'), slim.get_model_variables('resnet_v2_50')) elif preset_model == 'DeepLabV3-Res101': with slim.arg_scope( resnet_v2.resnet_arg_scope(weight_decay=weight_decay)): logits, end_points = resnet_v2.resnet_v2_101( inputs, is_training=is_training, scope='resnet_v2_101') resnet_scope = 'resnet_v2_101' # DeepLabV3 requires pre-trained ResNet weights init_fn = slim.assign_from_checkpoint_fn( os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'), slim.get_model_variables('resnet_v2_101')) elif preset_model == 'DeepLabV3-Res152': with slim.arg_scope( resnet_v2.resnet_arg_scope(weight_decay=weight_decay)): logits, end_points = resnet_v2.resnet_v2_152( inputs, is_training=is_training, scope='resnet_v2_152') resnet_scope = 'resnet_v2_152' # DeepLabV3 requires pre-trained ResNet weights init_fn = slim.assign_from_checkpoint_fn( os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'), slim.get_model_variables('resnet_v2_152')) else: raise ValueError( "Unsupported ResNet model '%s'. This function only supports ResNet 50, ResNet 101, and ResNet 152" % (preset_model)) label_size = tf.shape(inputs)[1:3] net = AtrousSpatialPyramidPoolingModule(end_points['pool5']) if upscaling_method.lower() == "conv": net = ConvUpscaleBlock(net, 256, kernel_size=[3, 3], scale=2) net = ConvBlock(net, 256) net = ConvUpscaleBlock(net, 128, kernel_size=[3, 3], scale=2) net = ConvBlock(net, 128) net = ConvUpscaleBlock(net, 64, kernel_size=[3, 3], scale=2) net = ConvBlock(net, 64) elif upscaling_method.lower() == "bilinear": net = Upsampling(net, label_size) net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, scope='logits') return net, init_fn
def main(_): print 'reading npy...' data = np.load('../data/1st.npy') jpg_list = [] for i in range(len(data)): jpg_list.append(str(i) + '.jpg') train_order = np.load('../data/train.npy') validation_order = np.load('../data/validation.npy') one_epoch_iter = len(train_order) / FLAGS.batch_size print 'reading finished' sess = tf.Session() arg_scope = resnet_v2.resnet_arg_scope() print 'building network...' with slim.arg_scope(arg_scope): hg = resnet.resnet(is_training=True) global_step = tf.Variable(0, name='global_step', trainable=False) learning_rate = tf.train.exponential_decay( FLAGS.learning_rate, global_step, 0.5 * FLAGS.max_epoch * len(train_order) / FLAGS.batch_size, 0.5, staircase=True) tf.summary.scalar('learning_rate', learning_rate) optimizer = tf.train.AdamOptimizer(learning_rate) init_fn = _get_init_fn() variables_to_train = _get_variables_to_train() update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_op = optimizer.minimize(hg.total_loss, global_step=global_step, var_list=variables_to_train) merged_summary = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.summary_dir, sess.graph) sess.run(tf.initialize_all_variables()) saver = tf.train.Saver(max_to_keep=3) init_fn(sess) print 'building finished' def train_step(input_image, input_label): feed_dict = {} feed_dict[hg.input_image] = input_image feed_dict[hg.input_label] = input_label temp, step, ce_loss, l2_loss, total_loss, summary, output = sess.run([ train_op, global_step, hg.ce_loss, hg.l2_loss, hg.total_loss, merged_summary, hg.output ], feed_dict) summary_writer.add_summary(summary, step) return output, ce_loss, l2_loss, total_loss def validation_step(current_step): print 'Validating...' all_ce_loss = 0 all_l2_loss = 0 all_total_loss = 0 all_output = [] all_label = [] for i in range(int(len(validation_order) / 18)): input_image = get_data.get_jpg_test( jpg_list, validation_order[18 * i:18 * (i + 1)]) input_label = get_data.get_label( data, validation_order[18 * i:18 * (i + 1)]) feed_dict = {} feed_dict[hg.input_image] = input_image feed_dict[hg.input_label] = input_label ce_loss, l2_loss, total_loss, output = sess.run( [hg.ce_loss, hg.l2_loss, hg.total_loss, hg.output], feed_dict) all_ce_loss += ce_loss all_l2_loss += l2_loss all_total_loss += total_loss for i in output: all_output.append(i) for i in input_label: all_label.append(i) all_output = np.array(all_output) all_label = np.array(all_label) auc = roc_auc_score(all_label, all_output) ce_loss = all_ce_loss / 283.0 l2_loss = all_l2_loss / 283.0 total_loss = all_total_loss / 283.0 all_output = np.reshape(all_output, (-1)) all_label = np.reshape(all_label, (-1)) ap = average_precision_score(all_label, all_output) time_str = datetime.datetime.now().isoformat() tempstr = "{}: auc {:g}, ap {:g}, ce_loss {:g}, l2_loss {:g}, total_loss {:g}".format( time_str, auc, ap, ce_loss, l2_loss, total_loss) print(tempstr) summary_writer.add_summary(MakeSummary('validation/auc', auc), current_step) summary_writer.add_summary(MakeSummary('validation/ap', ap), current_step) summary_writer.add_summary(MakeSummary('validation/ce_loss', ce_loss), current_step) return ce_loss best_ce_loss = 10000 best_iter = 0 smooth_ce_loss = 0 smooth_l2_loss = 0 smooth_total_loss = 0 temp_label = [] temp_output = [] for one_epoch in range(FLAGS.max_epoch): print('epoch ' + str(one_epoch + 1) + ' starts!') np.random.shuffle(train_order) for i in range(int(len(train_order) / float(FLAGS.batch_size))): start = i * FLAGS.batch_size end = (i + 1) * FLAGS.batch_size input_image = get_data.get_jpg_train(jpg_list, train_order[start:end]) input_label = get_data.get_label(data, train_order[start:end]) output, ce_loss, l2_loss, total_loss = train_step( input_image, input_label) smooth_ce_loss += ce_loss smooth_l2_loss += l2_loss smooth_total_loss += total_loss temp_label.append(input_label) temp_output.append(output) current_step = tf.train.global_step(sess, global_step) if current_step % 10 == 0: ce_loss = smooth_ce_loss / 10.0 l2_loss = smooth_l2_loss / 10.0 total_loss = smooth_total_loss / 10.0 temp_output = np.reshape(np.array(temp_output), (-1)) temp_label = np.reshape(np.array(temp_label), (-1)) ap = average_precision_score(temp_label, temp_output) temp_output = np.reshape(temp_output, (-1, 100)) temp_label = np.reshape(temp_label, (-1, 100)) auc = roc_auc_score(temp_label, temp_output) time_str = datetime.datetime.now().isoformat() tempstr = "{}: step {}, auc {:g}, ap {:g}, ce_loss {:g}, l2_loss {:g}, total_loss {:g}".format( time_str, current_step, auc, ap, ce_loss, l2_loss, total_loss) print(tempstr) summary_writer.add_summary(MakeSummary('train/auc', auc), current_step) summary_writer.add_summary(MakeSummary('train/ap', ap), current_step) temp_output = [] temp_label = [] smooth_ce_loss = 0 smooth_l2_loss = 0 smooth_total_loss = 0 if current_step % int(one_epoch_iter * FLAGS.save_epoch) == 0: ce_loss = validation_step(current_step) if ce_loss < best_ce_loss: print 'currently the validation ce_loss is less the previous best one!!!' best_ce_loss = ce_loss best_iter = current_step print 'saving model' path = saver.save(sess, FLAGS.model_dir + 'resnet_model', global_step=current_step) print 'have saved model to ' + path print 'warmup training has been finished !' print 'the best model iter is ' + str(best_iter) print 'the best ce_loss on validation is ' + str(best_ce_loss)
def build_icnet(inputs, label_size, num_classes, preset_model='ICNet-Res50', pooling_type = "MAX", weight_decay=1e-5, is_training=True, pretrained_dir="models"): """ Builds the ICNet model. Arguments: inputs: The input tensor label_size: Size of the final label tensor. We need to know this for proper upscaling preset_model: Which model you want to use. Select which ResNet model to use for feature extraction num_classes: Number of classes pooling_type: Max or Average pooling Returns: ICNet model """ inputs_4 = tf.image.resize_bilinear(inputs, size=[tf.shape(inputs)[1]*4, tf.shape(inputs)[2]*4]) inputs_2 = tf.image.resize_bilinear(inputs, size=[tf.shape(inputs)[1]*2, tf.shape(inputs)[2]*2]) inputs_1 = inputs if preset_model == 'ICNet-Res50': with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)): logits_32, end_points_32 = resnet_v2.resnet_v2_50(inputs_4, is_training=is_training, scope='resnet_v2_50') logits_16, end_points_16 = resnet_v2.resnet_v2_50(inputs_2, is_training=is_training, scope='resnet_v2_50') logits_8, end_points_8 = resnet_v2.resnet_v2_50(inputs_1, is_training=is_training, scope='resnet_v2_50') resnet_scope='resnet_v2_50' # ICNet requires pre-trained ResNet weights init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'), slim.get_model_variables('resnet_v2_50')) elif preset_model == 'ICNet-Res101': with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)): logits_32, end_points_32 = resnet_v2.resnet_v2_101(inputs_4, is_training=is_training, scope='resnet_v2_101') logits_16, end_points_16 = resnet_v2.resnet_v2_101(inputs_2, is_training=is_training, scope='resnet_v2_101') logits_8, end_points_8 = resnet_v2.resnet_v2_101(inputs_1, is_training=is_training, scope='resnet_v2_101') resnet_scope='resnet_v2_101' # ICNet requires pre-trained ResNet weights init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'), slim.get_model_variables('resnet_v2_101')) elif preset_model == 'ICNet-Res152': with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)): logits_32, end_points_32 = resnet_v2.resnet_v2_152(inputs_4, is_training=is_training, scope='resnet_v2_152') logits_16, end_points_16 = resnet_v2.resnet_v2_152(inputs_2, is_training=is_training, scope='resnet_v2_152') logits_8, end_points_8 = resnet_v2.resnet_v2_152(inputs_1, is_training=is_training, scope='resnet_v2_152') resnet_scope='resnet_v2_152' # ICNet requires pre-trained ResNet weights init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'), slim.get_model_variables('resnet_v2_152')) else: raise ValueError("Unsupported ResNet model '%s'. This function only supports ResNet 50, ResNet 101, and ResNet 152" % (preset_model)) feature_map_shape = [int(x / 32.0) for x in label_size] block_32 = PyramidPoolingModule(end_points_32['pool3'], feature_map_shape=feature_map_shape, pooling_type=pooling_type) out_16, block_16 = CFFBlock(psp_32, end_points_16['pool3']) out_8, block_8 = CFFBlock(block_16, end_points_8['pool3']) out_4 = Upsampling_by_scale(out_8, scale=2) out_4 = slim.conv2d(out_4, num_classes, [1, 1], activation_fn=None) out_full = Upsampling_by_scale(out_4, scale=2) out_full = slim.conv2d(out_full, num_classes, [1, 1], activation_fn=None, scope='logits') net = tf.concat([out_16, out_8, out_4, out_final]) return net, init_fn
def train(): train_dir = '/home/daijiaming/Galaxy/data3/trainset/' train_label_dir = '/home/daijiaming/Galaxy/data3/train_label.csv' test_dir = '/home/daijiaming/Galaxy/data3/testset/' test_label_dir = '/home/daijiaming/Galaxy/data3/test_label.csv' train_log_dir = '/home/daijiaming/Galaxy/GalaxyNet/logs/train/' val_log_dir = '/home/daijiaming/Galaxy/GalaxyNet/logs//val/' tra_image_batch, tra_label_batch, tra_galalxyid_batch = input_data.read_galaxy11( data_dir=train_dir, label_dir=train_label_dir, batch_size=BATCH_SIZE) val_image_batch, val_label_batch, val_galalxyid_batch = input_data.read_galaxy11_test( data_dir=test_dir, label_dir=test_label_dir, batch_size=BATCH_SIZE) x = tf.placeholder(tf.float32, [BATCH_SIZE, 64, 64, 3]) y_ = tf.placeholder(tf.float32, [BATCH_SIZE, N_CLASSES]) # keep_prob=tf.placeholder(tf.float32) with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, end_points, output0, output1 = resnet_v2.resnet_v2_26_2( x, N_CLASSES, is_training=True) loss = resnet_v2.loss(logits, y_) # rmse=resnet_v2.compute_rmse(logits, y_) accuracy = resnet_v2.accuracy(logits, y_) my_global_step = tf.Variable(0, name='global_step', trainable=False) train_op = resnet_v2.optimize(loss, learning_rate, my_global_step) saver = tf.train.Saver(tf.global_variables()) summary_op = tf.summary.merge_all() init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) tra_summary_writer = tf.summary.FileWriter(train_log_dir, sess.graph) val_summary_writer = tf.summary.FileWriter(val_log_dir, sess.graph) try: for step in np.arange(MAX_STEP): if coord.should_stop(): break tra_images, tra_labels = sess.run( [tra_image_batch, tra_label_batch]) _, tra_loss, tra_acc, summary_str = sess.run( [train_op, loss, accuracy, summary_op], feed_dict={ x: tra_images, y_: tra_labels }) if step % 50 == 0 or (step + 1) == MAX_STEP: print('Step: %d, tra_loss: %.4f, tra_accuracy: %.2f%%' % (step, tra_loss, tra_acc)) # summary_str = sess.run(summary_op,feed_dict={x:tra_images, y_:tra_labels}) tra_summary_writer.add_summary(summary_str, step) if step % 200 == 0 or (step + 1) == MAX_STEP: val_images, val_labels = sess.run( [val_image_batch, val_label_batch]) val_loss, val_acc, summary_str = sess.run( [loss, accuracy, summary_op], feed_dict={ x: val_images, y_: val_labels }) print( '** Step %d, test_loss = %.4f, test_accuracy = %.2f%% **' % (step, val_loss, val_acc)) # summary_str = sess.run([summary_op],feed_dict={x:val_images,y_:val_labels}) val_summary_writer.add_summary(summary_str, step) if step % 2000 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join(train_log_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def train(): N_CLASSES = 5 BATCH_SIZE =5 learning_rate = 0.1 MAX_STEP = 20 train_images = np.load('./Training_images_Preprocess2.npy', allow_pickle=True) train_labels = np.load("./Training_labels_Preprocess2.npy", allow_pickle=True) train_tf_images = [] train_tf_labels = [] for i in range(len(train_images)): train_tf_images.append(tf.convert_to_tensor(train_images[i], dtype=tf.float32)) train_tf_labels.append(tf.convert_to_tensor(train_labels[i], dtype=tf.int32)) val_images = np.load('./Val_images_Preprocess.npy', allow_pickle=True) val_labels = np.load("./Val_labels_Preprocess.npy", allow_pickle=True) val_tf_images = [] val_tf_labels = [] for i in range(len(val_images)): val_tf_images.append(tf.convert_to_tensor(val_images[i], dtype=tf.float32)) val_tf_labels.append(tf.convert_to_tensor(val_labels[i], dtype=tf.int32)) test_images = np.load('./Test_images_Preprocess.npy', allow_pickle=True) test_labels = np.load("./Test_labels_Preprocess.npy", allow_pickle=True) test_tf_images = [] test_tf_labels = [] for i in range(len(test_images)): test_tf_images.append(tf.convert_to_tensor(test_images[i], dtype=tf.float32)) test_tf_labels.append(tf.convert_to_tensor(test_labels[i], dtype=tf.int32)) def input_fn(train_data, train, batch_size=BATCH_SIZE,buffer_size=1024): dx = tf.data.Dataset.from_tensor_slices(train_data[0]) dy = tf.data.Dataset.from_tensor_slices(train_data[1]) dataset = tf.data.Dataset.zip((dx,dy)) if train: dataset = dataset.shuffle(buffer_size=buffer_size) num_repeat = None else: num_repeat = 1 dataset = dataset.repeat(num_repeat) dataset = dataset.batch(batch_size) iterator = tf.compat.v1.data.make_one_shot_iterator(dataset) images_batch, labels_batch = iterator.get_next() return images_batch,labels_batch def train_input_fn(): return input_fn([train_tf_images,train_tf_labels], train=True) def val_input_fn(): return input_fn([val_tf_images,val_tf_labels], train=True) x = tf.placeholder(tf.float32, [BATCH_SIZE, 64, 64, 3]) y_ = tf.placeholder(tf.float32, [BATCH_SIZE, N_CLASSES]) with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, end_points,output0,output1 = resnet_v2.resnet_v2_26_2(x, N_CLASSES, is_training=True) loss = resnet_v2.loss(logits, y_) accuracy = resnet_v2.accuracy(logits, y_) my_global_step = tf.Variable(0, name='global_step', trainable=False) train_op = resnet_v2.optimize(loss, learning_rate, my_global_step) saver = tf.train.Saver(tf.global_variables()) summary_op = tf.summary.merge_all() init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) tra_summary_writer = tf.summary.FileWriter('./', sess.graph) val_summary_writer = tf.summary.FileWriter('./', sess.graph) try: for step in np.arange(MAX_STEP): if coord.should_stop(): break tra_image_batch, tra_label_batch = train_input_fn() val_image_batch, val_label_batch = val_input_fn() tra_images,tra_labels = sess.run([tra_image_batch, tra_label_batch]) _,tra_loss,tra_acc,summary_str = sess.run([train_op,loss,accuracy,summary_op],feed_dict={x:tra_images, y_:tra_labels}) if step % 1 == 0 or (step + 1) == MAX_STEP: print ('Step: %d, tra_loss: %.4f, tra_accuracy: %.2f%%' % (step, tra_loss, tra_acc)) tra_summary_writer.add_summary(summary_str, step) if step % 5 == 0 or (step + 1) == MAX_STEP: val_images, val_labels = sess.run([val_image_batch, val_label_batch]) val_loss, val_acc, summary_str = sess.run([loss, accuracy,summary_op],feed_dict={x:val_images,y_:val_labels}) print('** Step %d, test_loss = %.4f, test_accuracy = %.2f%% **' %(step, val_loss, val_acc)) val_summary_writer.add_summary(summary_str, step) if step % 10 == 0 or (step + 1) == MAX_STEP: checkpoint_path = os.path.join('./', 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads) sess.close()
def build_gcn(inputs, num_classes, preset_model='GCN-Res101', weight_decay=1e-5, is_training=True, upscaling_method="bilinear", pretrained_dir="models"): """ Builds the GCN model. Arguments: inputs: The input tensor preset_model: Which model you want to use. Select which ResNet model to use for feature extraction num_classes: Number of classes Returns: GCN model """ inputs = mean_image_subtraction(inputs) if preset_model == 'GCN-Res50': with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)): logits, end_points = resnet_v2.resnet_v2_50(inputs, is_training=is_training, scope='resnet_v2_50') resnet_scope = 'resnet_v2_50' # GCN requires pre-trained ResNet weights init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'), slim.get_model_variables('resnet_v2_50')) elif preset_model == 'GCN-Res101': with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)): logits, end_points = resnet_v2.resnet_v2_101(inputs, is_training=is_training, scope='resnet_v2_101') resnet_scope = 'resnet_v2_101' # GCN requires pre-trained ResNet weights init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'), slim.get_model_variables('resnet_v2_101')) elif preset_model == 'GCN-Res152': with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)): logits, end_points = resnet_v2.resnet_v2_152(inputs, is_training=is_training, scope='resnet_v2_152') resnet_scope = 'resnet_v2_152' # GCN requires pre-trained ResNet weights init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'), slim.get_model_variables('resnet_v2_152')) else: raise ValueError("Unsupported ResNet model '%s'. This function only supports ResNet 101 and ResNet 152" % (preset_model)) res = [end_points['pool5'], end_points['pool4'], end_points['pool3'], end_points['pool2']] down_5 = GlobalConvBlock(res[0], n_filters=21, size=3) down_5 = BoundaryRefinementBlock(down_5, n_filters=21, kernel_size=[3, 3]) down_5 = ConvUpscaleBlock(down_5, n_filters=21, kernel_size=[3, 3], scale=2) down_4 = GlobalConvBlock(res[1], n_filters=21, size=3) down_4 = BoundaryRefinementBlock(down_4, n_filters=21, kernel_size=[3, 3]) down_4 = tf.add(down_4, down_5) down_4 = BoundaryRefinementBlock(down_4, n_filters=21, kernel_size=[3, 3]) down_4 = ConvUpscaleBlock(down_4, n_filters=21, kernel_size=[3, 3], scale=2) down_3 = GlobalConvBlock(res[2], n_filters=21, size=3) down_3 = BoundaryRefinementBlock(down_3, n_filters=21, kernel_size=[3, 3]) down_3 = tf.add(down_3, down_4) down_3 = BoundaryRefinementBlock(down_3, n_filters=21, kernel_size=[3, 3]) down_3 = ConvUpscaleBlock(down_3, n_filters=21, kernel_size=[3, 3], scale=2) down_2 = GlobalConvBlock(res[3], n_filters=21, size=3) down_2 = BoundaryRefinementBlock(down_2, n_filters=21, kernel_size=[3, 3]) down_2 = tf.add(down_2, down_3) down_2 = BoundaryRefinementBlock(down_2, n_filters=21, kernel_size=[3, 3]) down_2 = ConvUpscaleBlock(down_2, n_filters=21, kernel_size=[3, 3], scale=2) net = BoundaryRefinementBlock(down_2, n_filters=21, kernel_size=[3, 3]) net = ConvUpscaleBlock(net, n_filters=21, kernel_size=[3, 3], scale=2) net = BoundaryRefinementBlock(net, n_filters=21, kernel_size=[3, 3]) net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, scope='logits') return net, init_fn
def build_refinenet(inputs, num_classes, preset_model='RefineNet-Res101', weight_decay=1e-5, is_training=True, upscaling_method="bilinear", pretrained_dir="models"): """ Builds the RefineNet model. Arguments: inputs: The input tensor preset_model: Which model you want to use. Select which ResNet model to use for feature extraction num_classes: Number of classes Returns: RefineNet model """ if preset_model == 'RefineNet-Res50': with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)): logits, end_points = resnet_v2.resnet_v2_50(inputs, is_training=is_training, scope='resnet_v2_50') # RefineNet requires pre-trained ResNet weights init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'), slim.get_model_variables('resnet_v2_50')) elif preset_model == 'RefineNet-Res101': with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)): logits, end_points = resnet_v2.resnet_v2_101(inputs, is_training=is_training, scope='resnet_v2_101') # RefineNet requires pre-trained ResNet weights init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'), slim.get_model_variables('resnet_v2_101')) elif preset_model == 'RefineNet-Res152': with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)): logits, end_points = resnet_v2.resnet_v2_152(inputs, is_training=is_training, scope='resnet_v2_152') # RefineNet requires pre-trained ResNet weights init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'), slim.get_model_variables('resnet_v2_152')) else: raise ValueError("Unsupported ResNet model '%s'. This function only supports ResNet 101 and ResNet 152" % (preset_model)) high = [end_points['pool5'], end_points['pool4'], end_points['pool3'], end_points['pool2']] low = [None, None, None, None] # Get the feature maps to the proper size with bottleneck high[0]=slim.conv2d(high[0], 512, 1) high[1]=slim.conv2d(high[1], 256, 1) high[2]=slim.conv2d(high[2], 256, 1) high[3]=slim.conv2d(high[3], 256, 1) # RefineNet low[0]=RefineBlock(high_inputs=high[0],low_inputs=None) # Only input ResNet 1/32 low[1]=RefineBlock(high[1],low[0]) # High input = ResNet 1/16, Low input = Previous 1/16 low[2]=RefineBlock(high[2],low[1]) # High input = ResNet 1/8, Low input = Previous 1/8 low[3]=RefineBlock(high[3],low[2]) # High input = ResNet 1/4, Low input = Previous 1/4 # g[3]=Upsampling(g[3],scale=4) net = low[3] net = ResidualConvUnit(net) net = ResidualConvUnit(net) if upscaling_method.lower() == "conv": net = ConvUpscaleBlock(net, 128, kernel_size=[3, 3], scale=2) net = ConvBlock(net, 128) net = ConvUpscaleBlock(net, 64, kernel_size=[3, 3], scale=2) net = ConvBlock(net, 64) elif upscaling_method.lower() == "bilinear": net = Upsampling(net, scale=4) net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, scope='logits') return net, init_fn
def build_pspnet(inputs, label_size, num_classes, preset_model='PSPNet-Res50', pooling_type="MAX", weight_decay=1e-5, upscaling_method="conv", is_training=True, pretrained_dir="models"): """ Builds the PSPNet model. Arguments: inputs: The input tensor label_size: Size of the final label tensor. We need to know this for proper upscaling preset_model: Which model you want to use. Select which ResNet model to use for feature extraction num_classes: Number of classes pooling_type: Max or Average pooling Returns: PSPNet model """ if preset_model == 'PSPNet-Res50': with slim.arg_scope( resnet_v2.resnet_arg_scope(weight_decay=weight_decay)): logits, end_points = resnet_v2.resnet_v2_50( inputs, is_training=is_training, scope='resnet_v2_50') resnet_scope = 'resnet_v2_50' # PSPNet requires pre-trained ResNet weights init_fn = slim.assign_from_checkpoint_fn( os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'), slim.get_model_variables('resnet_v2_50')) elif preset_model == 'PSPNet-Res101': with slim.arg_scope( resnet_v2.resnet_arg_scope(weight_decay=weight_decay)): logits, end_points = resnet_v2.resnet_v2_101( inputs, is_training=is_training, scope='resnet_v2_101') resnet_scope = 'resnet_v2_101' # PSPNet requires pre-trained ResNet weights init_fn = slim.assign_from_checkpoint_fn( os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'), slim.get_model_variables('resnet_v2_101')) elif preset_model == 'PSPNet-Res152': with slim.arg_scope( resnet_v2.resnet_arg_scope(weight_decay=weight_decay)): logits, end_points = resnet_v2.resnet_v2_152( inputs, is_training=is_training, scope='resnet_v2_152') resnet_scope = 'resnet_v2_152' # PSPNet requires pre-trained ResNet weights init_fn = slim.assign_from_checkpoint_fn( os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'), slim.get_model_variables('resnet_v2_152')) else: raise ValueError( "Unsupported ResNet model '%s'. This function only supports ResNet 50, ResNet 101, and ResNet 152" % (preset_model)) feature_map_shape = [int(x / 8.0) for x in label_size] print(feature_map_shape) psp = PyramidPoolingModule(end_points['pool3'], feature_map_shape=feature_map_shape, pooling_type=pooling_type) net = slim.conv2d(psp, 512, [3, 3], activation_fn=None) net = slim.batch_norm(net, fused=True) net = tf.nn.relu(net) if upscaling_method.lower() == "conv": net = ConvUpscaleBlock(net, 256, kernel_size=[3, 3], scale=2) net = ConvBlock(net, 256) net = ConvUpscaleBlock(net, 128, kernel_size=[3, 3], scale=2) net = ConvBlock(net, 128) net = ConvUpscaleBlock(net, 64, kernel_size=[3, 3], scale=2) net = ConvBlock(net, 64) elif upscaling_method.lower() == "bilinear": net = Upsampling(net, label_size) net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, scope='logits') return net, init_fn
def evaluate(): with tf.Graph().as_default(): log_dir = DATA_DIR + 'logs/train/' test_dir = DATA_DIR + 'testset/' test_label_dir = DATA_DIR + 'test_label.csv' # what is the n_test? change for image that we have n_test = 2879 val_image_batch, val_label_batch = input_data.read_galaxy11_test( data_dir=test_dir, label_dir=test_label_dir, batch_size=BATCH_SIZE) x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, 64, 64, 3]) y_ = tf.placeholder(tf.int32, shape=[BATCH_SIZE, N_CLASSES]) with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, end_points, output0, output1 = resnet_v2.resnet_v2_26_2( x, N_CLASSES, is_training=True) correct = resnet_v2.num_correct_prediction(logits, y_) accuracy = resnet_v2.accuracy(logits, y_) # top_k_op = tf.nn.in_top_k(predictions=logits,targets=y_, k=1) saver = tf.train.Saver(tf.global_variables()) with tf.Session() as sess: sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) print("Reading checkpoints...") ckpt = tf.train.get_checkpoint_state(log_dir) if ckpt and ckpt.model_checkpoint_path: global_step = ckpt.model_checkpoint_path.split('/')[-1].split( '-')[-1] saver.restore(sess, ckpt.model_checkpoint_path) print('Loading success, global_step is %s' % global_step) else: print('No checkpoint file found') return coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: print('\nEvaluating......') num_step = int(math.ceil(n_test / BATCH_SIZE)) num_sample = num_step * BATCH_SIZE step = 0 total_correct = 0 totall_acc = 0.0 # true_count = 0 while step < num_step and not coord.should_stop(): test_images, test_labels = sess.run( [val_image_batch, val_label_batch]) batch_correct, tes_acc, batch_logits = sess.run( [correct, accuracy, logits], feed_dict={ x: test_images, y_: test_labels }) # print('tes_acc = %.3f' % tes_acc) total_correct += np.sum(batch_correct) totall_acc = totall_acc + tes_acc # print('totall_acc = %.3f' % totall_acc) # true_count += np.sum(predictions) if step == 0: a = test_labels b = batch_logits if step >= 1: a = np.concatenate((a, test_labels)) b = np.concatenate((b, batch_logits)) step += 1 # precision = true_count / num_sample aver_acc = totall_acc / num_step print('Aver acc = %.4f' % aver_acc) print('Total testing samples: %d' % num_sample) print('Total correct predictions: %d' % total_correct) print('Average accuracy: %.4f%%' % (100 * total_correct / num_sample)) np.savetxt('labels2879.csv', a, delimiter=',') np.savetxt('logits2879.csv', b, delimiter=',') except Exception as e: coord.request_stop(e) finally: coord.request_stop() coord.join(threads)
def train(flag, args): tf.reset_default_graph() queue_loader = data_loader(flag, batch_size=args.bsize, num_epochs=args.ep, dataset_dir=args.dataset_dir) with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, end_points = resnet_v2.resnet_v2_101(queue_loader.images, 764, is_training=flag) g_step = tf.Variable(0, trainable=False, dtype=tf.int64, name='g_step') # g_step = tf.train.create_global_step() decay_steps = int(math.floor(43971 / args.bsize * 2.0)) lr = tf.train.exponential_decay(args.lr, global_step=g_step, decay_steps=decay_steps, decay_rate=0.94, staircase=True) # loss_AuxLogits = tf.losses.sparse_softmax_cross_entropy( labels=queue_loader.labels, logits=end_points['AuxLogits'], weights=0.4) total_loss = tf.losses.sparse_softmax_cross_entropy( labels=queue_loader.labels, logits=logits, weights=1.0) # total_loss = tf.losses.get_total_loss(add_regularization_losses=False) # train_op = tf.train.RMSPropOptimizer(lr).minimize(total_loss) labels = queue_loader.labels correct = tf.equal(tf.argmax(logits, 1), labels) accuracy = tf.reduce_mean(tf.cast(correct, tf.float32)) # tf.summary.scalar('AuxLogits loss', loss_AuxLogits) tf.summary.scalar('accuracy', accuracy) tf.summary.scalar('total loss', total_loss) tf.summary.scalar('learnning_rate', lr) merged_op = tf.summary.merge_all() # for var in slim.get_model_variables(): # if var.op.name.startswith("Variabl"): # print(var) with tf.variable_scope('Adam_vars'): # optimizer = tf.train.AdamOptimizer(lr).minimize(loss_op,global_step=global_step) optimizer = tf.train.AdamOptimizer(lr) grads_vars_tuple_list = optimizer.compute_gradients(total_loss) grads, vars = zip(*grads_vars_tuple_list) new_grads, _ = tf.clip_by_global_norm(grads, 5) new_grads_vars_tuple_list = zip(new_grads, vars) train_op = optimizer.apply_gradients(new_grads_vars_tuple_list, global_step=g_step) # optimizer = tf.train.AdamOptimizer(lr) # grads_and_vars = optimizer.compute_gradients(loss_op) # for i,(grad,var) in enumerate(grads_and_vars): # if grad is not None: # grads_and_vars[i] = (tf.clip_by_norm(grad,5),var) # train_op = optimizer.apply_gradients(grads_and_vars,global_step=global_step) if len(os.listdir(args.modelpath)) > 0: var_to_restore = slim.get_model_variables() ckpt_path = tf.train.latest_checkpoint(args.modelpath) print('###########################ckpt_path', ckpt_path) else: # var_to_restore = slim.get_variables_to_restore(exclude=['InceptionV3/AuxLogits/Conv2d_1b_1x1', # 'InceptionV3/Logits/Conv2d_1b_1x1/Conv2d_1c_1x1']) var_to_restore = slim.get_variables_to_restore( exclude=['resnet_v2_101/logits', 'Adam_vars', 'g_step']) # 'InceptionV4/AuxLogits/Conv2d_1b_1x1/BatchNorm/beta/Adam', # 'InceptionV4/AuxLogits/Conv2d_1b_1x1/weights/Adam' # Aux_logit = slim.get_variables_to_restore(include=['InceptionV4/AuxLogits/Aux_logits']) # Logit = slim.get_variables_to_restore(include=['InceptionV4/Logits/Logits']) # adam = slim.get_variables_to_restore(include=['Adam_vars']) ckpt_path = tf.train.latest_checkpoint(args.dataset_dir2) print('###########################ckpt_path', ckpt_path) # Aux_logit_init = tf.variables_initializer(Aux_logit) # Logit_init = tf.variables_initializer(Logit) # adam_init = tf.variables_initializer(adam) init_func = slim.assign_from_checkpoint_fn(ckpt_path, var_to_restore) saver = tf.train.Saver(max_to_keep=1) config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) writer = tf.summary.FileWriter(args.trainlog, sess.graph) sess.run( tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())) # sess.run(Aux_logit_init) # sess.run(Logit_init) # sess.run(adam_init) init_func(sess) print( '###################################### restore checkpoint sucessful#######################' ) print('Start training') print('batch size: %d, epoch: %d, initial learning rate: %.3f' % (args.bsize, args.ep, args.lr)) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: ep = 1 correct_all = 0 start = datetime.now() while not coord.should_stop(): # logit, loss, correct_list, _, summary,acc,g_s= sess.run([ # logits,loss_op,correct, train_op, merged_op,accuracy,global_step]) logit, loss, correct_list, _, summary, acc, g_s, l = sess.run([ logits, total_loss, correct, train_op, merged_op, accuracy, g_step, lr ]) writer.add_summary(summary, g_s) # print('argmax logits',np.argmax(logits,1),sess.run(queue_loader.labels)) # print('correct.sum : ',correct.sum()) correct_all += correct_list.sum() # step_accuracy = correct_list.sum()*100.0/args.bsize if g_s % 10 == 0: end_time = datetime.now() print( 'epoch: %2d, globle_step: %3d,accuracy : %.2f%%,loss: %.3f, cost time : %s sec' % (ep, g_s, acc * 100.0, loss, end_time - start)) start = datetime.now() if g_s != 0 and g_s % queue_loader.num_batches == 0: print('EPOCH %2d is end, ACCURACY: %.2f%%.' % (ep, correct_all * 100.0 / (queue_loader.num_batches * args.bsize))) saver.save(sess, os.path.join(args.modelpath, 'model.ckpt'), global_step=g_s) ep += 1 correct_all = 0 except tf.errors.OutOfRangeError: print('\nDone training, epoch limit: %d reached.' % (ep)) finally: coord.request_stop() coord.join(threads) sess.close() print('Done')
def main(_): print 'reading npy...' #trainlist, labels = read_csv.train_data() #jpg_list = np.load('../jpg_1st.npy') data = np.load('../1st.npy') jpg_list=[] for i in range(len(data)): jpg_list.append(str(i)+'.jpg') #train_order = np.load('../train.npy') #test_order = np.load('../test.npy') test_order = [] for i in range(len(data)): test_order.append(i) sess = tf.Session() arg_scope = resnet_v2.resnet_arg_scope() print 'building network...' with slim.arg_scope(arg_scope): hg = resnet_test.resnet(is_training=False) init_fn = _get_init_fn() merged_summary = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.summary_dir,sess.graph) sess.run(tf.initialize_all_variables()) saver = tf.train.Saver(max_to_keep=None) init_fn(sess) print 'building finished' def test_step(): print 'testing...' # all_ce_loss = 0 # all_l2_loss = 0 # all_total_loss = 0 # all_output = [] # all_label = [] all_feature = [] batch_size=17*3 for i in range(int(len(test_order)/batch_size)): input_image = get_data.get_jpg_test(jpg_list,test_order[batch_size*i:batch_size*(i+1)]) input_label = get_data.get_label(data,test_order[batch_size*i:batch_size*(i+1)]) feed_dict={} feed_dict[hg.input_image]=input_image feature = sess.run(hg.feature,feed_dict) for i in feature: all_feature.append(i) # for i in input_label: # all_label.append(i) # all_output = np.array(all_output) # all_label = np.array(all_label) # #average_precision = average_precision_score(all_label,all_output) # np.save('output.npy',all_output) # np.save('label.npy',all_label) # auc = roc_auc_score(all_label,all_output) # loglike = log_likelihood(all_label,all_output) # time_str = datetime.datetime.now().isoformat() # tempstr = "{}: auc {:g}, log_likelihood {:g}".format(time_str, auc, loglike) # print(tempstr) # all_output=np.reshape(all_output,(-1)) # all_label=np.reshape(all_label,(-1)) # ap = average_precision_score(all_label,all_output) # auc_2 = roc_auc_score(all_label,all_output) # print 'ap:'+str(ap) # print 'auc_2:'+str(auc_2) np.save('resnet50_feature.npy',all_feature) test_step()