def extract_features_resnet50(self, im, name, is_training=True, reuse=False): use_global_pool = True num_classes = 4096 if use_global_pool else 512 with tf.name_scope(name): with slim.arg_scope(resnet_utils.resnet_arg_scope()): out, _ = resnet_v2.resnet_v2_50(inputs=im, num_classes=num_classes, global_pool=use_global_pool, is_training=self.is_training, spatial_squeeze=True, scope='resnet_v2_50', reuse=reuse) if not use_global_pool: args = { 'reuse': reuse, 'norm': None, 'activation': tf.nn.relu, 'padding': 'SAME', 'is_training': is_training } out_args = copy.deepcopy(args) out_args['activation'] = None out = ops.conv(out, 1024, 3, 2, name='conv1', **args) out = slim.batch_norm(out) out = ops.conv(out, 2048, 3, 2, name='conv2', **args) out = slim.batch_norm(out) out = ops.conv(out, 4096, 3, 2, name='conv3', **out_args) out = slim.batch_norm(out) out = tf.squeeze(out, [1, 2], name='SpatialSqueeze') return out
def test_resnet_v2_50(img_dir): """ Test ResNet-V1-50 with a single image. :param img_dir: Path of the image to be classified :return: classification result and probability of a single image """ img = cv2.imread(img_dir) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.resize(img, (224, 224)) / 255 img = img.reshape((1, 224, 224, 3)) tf.reset_default_graph() inputs = tf.placeholder(name='input_images', shape=[None, 224, 224, 3], dtype=tf.float32) with slim.arg_scope(resnet_arg_scope()): _, _ = resnet_v2_50(inputs, 1001, is_training=False) with tf.Session() as sess: tf.train.Saver().restore(sess, './models/resnet_v2_50.ckpt') inputs = sess.graph.get_tensor_by_name('input_images:0') outputs = sess.graph.get_tensor_by_name( 'resnet_v2_50/SpatialSqueeze:0') pred = tf.argmax(tf.nn.softmax(outputs), axis=1)[0] prob = tf.reduce_max(tf.nn.softmax(outputs), axis=1)[0] pred, prob = sess.run([pred, prob], feed_dict={inputs: img}) name = label_dict[pred] print('Result of ResNet-V1-50:', name, prob) return name, prob
def graph(x, y, i, x_max, x_min, grad, eps_inside): num_iter = FLAGS.num_iter alpha = eps_inside / num_iter momentum = FLAGS.momentum num_classes = 1001 with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits_resnet, end_points_resnet = resnet_v2.resnet_v2_50( x, num_classes=num_classes, is_training=False) pred = tf.argmax(end_points_resnet['predictions'], 1) first_round = tf.cast(tf.equal(i, 0), tf.int64) y = first_round * pred + (1 - first_round) * y one_hot = tf.one_hot(y, num_classes) logits = logits_resnet cross_entropy = tf.losses.softmax_cross_entropy(one_hot, logits, label_smoothing=0.0, weights=1.0) noise = tf.gradients(cross_entropy, x)[0] x = x + alpha * tf.sign(noise) x = tf.clip_by_value(x, x_min, x_max) i = tf.add(i, 1) return x, y, i, x_max, x_min, noise, eps_inside
def _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, 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, 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
def __init__(self): slim = tf.contrib.slim CLASSES = ['anger', ' happy ', 'neutral', ' sad ', 'surprise'] image_size = 160 checkpoints_dir = '/root/catkin_ws/src/ros_emotion_detect/src/models/inception_5/' logging.basicConfig(filename='result.log', filemode='w', level=logging.INFO) self.logger = logging.getLogger('emotion classifier') # loading model with tf.Graph().as_default(): self.image = tf.placeholder(tf.uint8, [None, None, 3]) self.processed_image = inception_preprocessing.preprocess_image( self.image, image_size, image_size, is_training=False) self.processed_images = tf.placeholder( tf.float32, [None, image_size, image_size, 3]) with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, _ = resnet_v2.resnet_v2_50(self.processed_images, num_classes=len(CLASSES), is_training=False) self.probs = tf.nn.softmax(logits) init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'model.ckpt-60000'), slim.get_model_variables('resnet_v2_50')) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True self.sess = tf.Session(config=config) init_fn(self.sess)
def build_train_op(image_tensor, label_tensor, is_training): resnet_argscope = resnet_arg_scope(weight_decay=FLAGS.weight_decay) global_step = tf.get_variable(name="global_step", shape=[], dtype=tf.int32, trainable=False) with slim.arg_scope(resnet_argscope): logits, end_points = resnet_v2_50(image_tensor, is_training=is_training, num_classes=100) loss = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=label_tensor)) accuracy = tf.reduce_sum( tf.cast( tf.equal(tf.cast(tf.argmax(logits, 1), tf.int32), label_tensor), tf.int32)) end_points['loss'], end_points['accuracy'] = loss, accuracy if is_training: optimizer = tf.train.AdadeltaOptimizer( learning_rate=FLAGS.learning_rate) train_op = optimizer.minimize(loss, global_step=global_step) return train_op, end_points else: return None, end_points
def hcd_model(inputs, num_classes, is_training=True, keep_prob=0.8, attention_module=None, scope='HCD_model'): ''' :param inputs: N x H x W x C tensor :return: ''' # with tf.variable_scope(scope, 'HCD_model', [inputs]): with slim.arg_scope(resnet_v2.resnet_arg_scope()): net, end_points = \ resnet_v2.resnet_v2_50(inputs, num_classes=num_classes, is_training=is_training, attention_module=attention_module, scope='resnet_v2_50') # out1 = GlobalMaxPooling2D()(x) net1 = tf.reduce_max(net, axis=[1, 2], keep_dims=True, name='GlobalMaxPooling2D') # out2 = GlobalAveragePooling2D()(x) net2 = tf.reduce_mean(net, axis=[1, 2], keep_dims=True, name='GlobalAveragePooling2D') # out3 = Flatten()(x) # net3 = slim.flatten(net) # out = Concatenate(axis=-1)([out1, out2, out3]) net = tf.concat([net1, net2], axis=-1) net = tf.squeeze(net, [1, 2], name='SpatialSqueeze') batch_norm_params['is_training'] = is_training # out = Dropout(0.5)(out) net = slim.dropout(net, keep_prob=keep_prob, is_training=is_training) # out = Dense(1, activation="sigmoid", name="3_")(out) net = slim.fully_connected(net, 768, normalizer_fn=slim.batch_norm, normalizer_params=batch_norm_params, scope='fc1') net = slim.dropout(net, keep_prob=keep_prob, is_training=is_training) net = slim.fully_connected(net, 256, normalizer_fn=slim.batch_norm, normalizer_params=batch_norm_params, scope='fc2') net = slim.dropout(net, keep_prob=keep_prob, is_training=is_training) logits = slim.fully_connected(net, num_classes, activation_fn=None, scope='logits') return logits, end_points
def inference_resnet_v2_50(x_input, dropout_keep_prob=1, num_classes=1001): with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, _ = resnet_v2.resnet_v2_50(x_input, num_classes=num_classes, is_training=False) probs = tf.nn.softmax(logits) model_vars = [var for var in tf.global_variables() \ if var.name.startswith('resnet_v2_50/')] return probs, logits, model_vars
def choose_model(x, model): """ 选择模型 :param x: :param model: :return: """ # 模型保存路径,模型名,预训练文件路径,前向传播 if model == 'Alex': log_dir = "E:/alum/log/Alex" y, _ = alexnet.alexnet_v2( x, num_classes=CLASSES, # 分类的类别 is_training=True, # 是否在训练 dropout_keep_prob=1.0, # 保留比率 spatial_squeeze=True, # 压缩掉1维的维度 global_pool=GLOBAL_POOL) # 输入不是规定的尺寸时,需要global_pool elif model == 'VGG': log_dir = "E:/alum/log/VGG" y, _ = vgg.vgg_16(x, num_classes=CLASSES, is_training=True, dropout_keep_prob=1.0, spatial_squeeze=True, global_pool=GLOBAL_POOL) elif model == 'VGG2': log_dir = "E:/alum/log/VGG2" y, _ = vgg.vgg_16(x, num_classes=CLASSES, is_training=True, dropout_keep_prob=1.0, spatial_squeeze=True, global_pool=GLOBAL_POOL) elif model == 'Incep4': log_dir = "E:/alum/log/Incep4" y, _ = inception_v4.inception_v4(x, num_classes=CLASSES, is_training=True, dropout_keep_prob=1.0, reuse=None, scope='InceptionV4', create_aux_logits=True) elif model == 'Res': log_dir = "E:/alum/log/Res" y, _ = resnet_v2.resnet_v2_50(x, num_classes=CLASSES, is_training=True, global_pool=GLOBAL_POOL, output_stride=None, spatial_squeeze=True, reuse=None, scope='resnet_v2_50') else: print('Error: model name not exist') return return y, log_dir
def resnet_v2_50(inputs): with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, end_points = resnet_v2.resnet_v2_50( inputs, num_classes=None, is_training=False, global_pool=True, spatial_squeeze=True) return logits, end_points, resnet_v2_50_ckpt_path
def graph(x, y, i, x_max, x_min, grad): eps = 2.0 * FLAGS.max_epsilon / 255.0 num_iter = FLAGS.num_iter alpha = eps / num_iter momentum = FLAGS.momentum num_classes = 1001 with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_v3, end_points_v3 = inception_v3.inception_v3( input_diversity(x), num_classes=num_classes, is_training=False) with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_adv_v3, end_points_adv_v3 = inception_v3.inception_v3( input_diversity(x), num_classes=num_classes, is_training=False, scope='AdvInceptionV3') with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits_v4, end_points_v4 = inception_v4.inception_v4( input_diversity(x), num_classes=num_classes, is_training=False) with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_res_v2, end_points_res_v2 = inception_resnet_v2.inception_resnet_v2( input_diversity(x), num_classes=num_classes, is_training=False) with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_ensadv_res_v2, end_points_ensadv_res_v2 = inception_resnet_v2.inception_resnet_v2( input_diversity(x), num_classes=num_classes, is_training=False, scope='EnsAdvInceptionResnetV2') with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits_resnet, end_points_resnet = resnet_v2.resnet_v2_50( input_diversity(x), num_classes=num_classes, is_training=False) logits = (logits_v3 + 0.25 * logits_adv_v3 + logits_v4 + \ logits_res_v2 + logits_ensadv_res_v2 + logits_resnet) / 5.25 auxlogits = (end_points_v3['AuxLogits'] + 0.25 * end_points_adv_v3['AuxLogits'] + end_points_v4['AuxLogits'] + \ end_points_res_v2['AuxLogits'] + end_points_ensadv_res_v2['AuxLogits']) / 4.25 cross_entropy = tf.losses.softmax_cross_entropy(y, logits, label_smoothing=0.0, weights=1.0) cross_entropy += tf.losses.softmax_cross_entropy(y, auxlogits, label_smoothing=0.0, weights=0.4) noise = tf.gradients(cross_entropy, x)[0] noise = tf.nn.depthwise_conv2d(noise, stack_kernel, strides=[1, 1, 1, 1], padding='SAME') noise = noise / tf.reshape(tf.contrib.keras.backend.std(tf.reshape(noise, [FLAGS.batch_size, -1]), axis=1), [FLAGS.batch_size, 1, 1, 1]) noise = momentum * grad + noise noise = noise / tf.reshape(tf.contrib.keras.backend.std(tf.reshape(noise, [FLAGS.batch_size, -1]), axis=1), [FLAGS.batch_size, 1, 1, 1]) x = x - alpha * tf.clip_by_value(tf.round(noise), -2, 2) x = tf.clip_by_value(x, x_min, x_max) i = tf.add(i, 1) return x, y, i, x_max, x_min, noise
def endpoints(image, is_training): if image.get_shape().ndims != 4: raise ValueError('Input must be of size [batch, height, width, 3]') image = image - tf.constant(_RGB_MEAN, dtype=tf.float32, shape=(1,1,1,3)) with tf.contrib.slim.arg_scope(resnet_arg_scope(batch_norm_decay=0.9, weight_decay=0.0)): _, endpoints = resnet_v2_50(image, num_classes=None, is_training=is_training, global_pool=True) endpoints['model_output'] = endpoints['global_pool'] = tf.reduce_mean( endpoints['resnet_v2_50/block4'], [1, 2], name='pool5', keep_dims=False) return endpoints, 'resnet_v2_50'
def fcn_8s_resnet_v2_50(x, num_classes=1000, is_training=False, weight_decay=0.0005, ): with tf.variable_scope('resnet50_fcn_8s'): with slim.arg_scope(resnet_arg_scope(weight_decay=weight_decay)): _, end_points = resnet_v2_50(x, num_classes=num_classes, is_training=is_training, global_pool=False, spatial_squeeze=False, ) with tf.variable_scope('conv_transpose_1'): net = conv2d_transpose(end_points['resnet50_fcn_8s/resnet_v2_50/logits'], filter_size=(4, 4, num_classes, end_points['resnet50_fcn_8s/resnet_v2_50/logits'].get_shape()[3]), output_shape=[tf.shape(end_points['resnet50_fcn_8s/resnet_v2_50/block2'])[0], tf.shape(end_points['resnet50_fcn_8s/resnet_v2_50/block2'])[1], tf.shape(end_points['resnet50_fcn_8s/resnet_v2_50/block2'])[2], num_classes], strides=2, weight_decay=weight_decay) pool4_conv = slim.conv2d(end_points['resnet50_fcn_8s/resnet_v2_50/block2'], num_classes, [3, 3]) net = tf.add(net, pool4_conv) end_points['resnet50_fcn_8s/conv_transpose_1'] = net with tf.variable_scope('conv_transpose_2'): net = conv2d_transpose(net, filter_size=(4, 4, num_classes, net.get_shape()[3]), output_shape=[tf.shape(end_points['resnet50_fcn_8s/resnet_v2_50/block1'])[0], tf.shape(end_points['resnet50_fcn_8s/resnet_v2_50/block1'])[1], tf.shape(end_points['resnet50_fcn_8s/resnet_v2_50/block1'])[2], num_classes], strides=2, weight_decay=weight_decay) pool3_conv = slim.conv2d(end_points['resnet50_fcn_8s/resnet_v2_50/block1'], num_classes, [3, 3]) net = tf.add(net, pool3_conv) end_points['resnet50_fcn_8s/conv_transpose_2'] = net with tf.variable_scope('conv_transpose_3'): net = conv2d_transpose(net, filter_size=(16, 16, num_classes, net.get_shape()[3]), output_shape=(tf.shape(x)[0], tf.shape(x)[1], tf.shape(x)[2], num_classes), strides=8, weight_decay=weight_decay) end_points['resnet50_fcn_8s/conv_transpose_3'] = net return net, end_points
def resnet_v2_50(inputs, is_training, opts): with slim.arg_scope(resnet_v2.resnet_arg_scope( weight_decay=opts.weight_decay, batch_norm_decay=opts.batch_norm_decay, batch_norm_epsilon=opts.batch_norm_epsilon, activation_fn=tf.nn.relu)): return resnet_v2.resnet_v2_50( inputs, num_classes=opts.num_classes, is_training=is_training, global_pool=opts.global_pool, output_stride=None, spatial_squeeze=opts.spatial_squeeze, reuse=None)
def create(self, images, num_classes, is_training): """See baseclass.""" with slim.arg_scope(resnet_v2.resnet_arg_scope()): _, endpoints = resnet_v2.resnet_v2_50( images, num_classes, is_training=is_training, spatial_squeeze=False) # Resnet's "predictions" endpoint is (n, 1, 1, m) but we really # want to have an (n, m) "Predictions" endpoint. We add a squeeze # op here to make that happen. endpoints['Predictions'] = tf.squeeze( endpoints['predictions'], [1, 2], name='SqueezePredictions') # Likewise, the endpoint "resnet_v2_50/logits" should be squeezed to # "Logits" endpoints['Logits'] = tf.squeeze( endpoints['resnet_v2_50/logits'], [1, 2], name='SqueezeLogits') return endpoints
def create(self, images, num_classes, is_training): """See baseclass.""" with slim.arg_scope(resnet_v2.resnet_arg_scope()): _, endpoints = resnet_v2.resnet_v2_50( images, num_classes, is_training=is_training, spatial_squeeze=False) # Resnet's "predictions" endpoint is (n, 1, 1, m) but we really # want to have an (n, m) "Predictions" endpoint. We add a squeeze # op here to make that happen. endpoints['Predictions'] = tf.squeeze( endpoints['predictions'], [1, 2], name='SqueezePredictions') # Likewise, the endpoint "resnet_v2_50/logits" should be squeezed to # "Logits" endpoints['Logits'] = tf.squeeze( endpoints['resnet_v2_50/logits'], [1, 2], name='SqueezeLogits') return endpoints
def resnetv2_ssd(img): with slim.arg_scope(resnet_v2.resnet_arg_scope()): net, end_points = resnet_v2.resnet_v2_50(img, is_training=True) c1 = end_points['resnet_v2_50/block1'] c2 = end_points['resnet_v2_50/block2'] base_16_0 = end_points['resnet_v2_50/block3'] base_16_1 = end_points['resnet_v2_50/block4'] vbs = slim.get_trainable_variables() # vbs = None base_16_0 = slim.conv2d(base_16_0, 512, 1) base_16_1 = slim.conv2d(base_16_1, 512, 1) c3 = tf.concat([base_16_0, base_16_1], axis=3) return c1, c2, c3, vbs
def __call__(self, x_input, batch_size=None, is_training=False): """Constructs model and return probabilities for given input.""" reuse = True if self.built else None with slim.arg_scope(resnet_utils.resnet_arg_scope()): with tf.variable_scope(self.ckpt): logits, end_points = resnet_v2.resnet_v2_50( x_input, num_classes=self.num_classes, is_training=is_training, reuse=reuse) preds = tf.argmax(logits, axis=1) self.built = True self.logits = logits self.preds = preds return logits
def init_network(self): x = self.image # x = tf.image.resize_images(x, [self.size,self.size], 0) #0 mean bilinear x = tf.image.resize_images(x, [self.height, self.width], 0) x = tf.subtract(x, 0.5) x = tf.multiply(x, 2.0) net, end_points = resnet_v2.resnet_v2_50( x, is_training=self.is_training, global_pool=self.global_pool, output_stride=self.output_stride, spatial_squeeze=self.spatial_squeeze, num_classes=None, reuse=self.reuse, scope='resnet_v2_50') net, end_points = pcb.pcb_net(net, end_points, self.num_classes, feature_dim=2048, only_pcb=FLAGS.only_pcb) # pdb.set_trace() # self.logits = end_points['resnet_v2_50/branch_0/resnet_v2_50/spatial_squeeze'] self.logits = end_points["Logits"] # self.pred = end_points['predictions'] self.pred = tf.reduce_mean([ end_points['predictions_0'], end_points['predictions_1'], end_points['predictions_2'], end_points['predictions_3'], end_points['predictions_4'], end_points['predictions_5'] ], axis=0) self.end_points = end_points corr_pred = tf.equal(tf.argmax(self.label, 1), tf.argmax(self.pred, 1)) self.acc = tf.reduce_sum(tf.cast(corr_pred, tf.int32)) for end_point in self.end_points: x = self.end_points[end_point] tf.summary.histogram('activations/%s/%s' % (self.scope, end_point), x) tf.summary.scalar('sparsity/%s/%s' % (self.scope, end_point), tf.nn.zero_fraction(x)) tf.summary.scalar('acc/%s' % self.acc, self.acc)
def get_resnet_v2_50_model(x, args, is_training): with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=args.weight_decay, batch_norm_decay=0.997, batch_norm_epsilon=1e-5, batch_norm_scale=True, activation_fn=tf.nn.relu, use_batch_norm=True)): _, end_points = resnet_v2.resnet_v2_50(x, num_classes=None, is_training=is_training, global_pool=False, output_stride=None, spatial_squeeze=True, reuse=None, scope='resnet_v2_50') net = end_points['resnet_v2_50/block4'] return get_classifier_and_reconstruct_model(net, args, is_training, end_points)
def train(): with tf.Graph().as_default(): tf.logging.set_verbosity(tf.logging.INFO) # not showing INFO logs dataset = flowers.get_split( 'train', flowers_data_dir ) # TODO Add directory of dataset, Check for format of dataset! images, _, labels = load_batch(dataset, height=image_size, width=image_size) # Create the model, use the default arg scope to configure the batch norm parameters. with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, _ = resnet_v2.resnet_v2_50( images, num_classes=dataset.num_classes, is_training=True) # TODO Choose Model (50, 101, 152, ...) # Specify the loss function: one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes) tf.losses.softmax_cross_entropy(logits, one_hot_labels) total_loss = tf.losses.get_total_loss() # Create some summaries to visualize the training process: tf.summary.scalar('losses/Total Loss', total_loss) #TODO Testing learning rate decay #starter_learning_rate = 0.01 #learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step, # 100000, 0.96, staircase=True) # Specify the optimizer and create the train op: optimizer = tf.train.MomentumOptimizer( learning_rate=learning_rate, momentum=0.9 ) # TODO add lowering of learning_rate, add weight decay (resnet_utils: weight_decay -> 0.0002) train_op = slim.learning.create_train_op(total_loss, optimizer) # Run the training: final_loss = slim.learning.train(train_op, logdir=train_dir, init_fn=get_init_fn(), number_of_steps=1030) print('Finished training. Last batch loss %f' % final_loss)
def my_cnn(images, is_training, dropout_rate =0.5): """ Creates a neural network and calculates the logits of a given set of images Args: images: batch of images of which logits need to be calculated is_training: boolean, indicates wether softmax should be calculated or not Returns: logits: logits of images """ # Create the model, use the default arg scope to configure the batch norm parameters. # TODO resnet_v2: change SpatialSqueeze back to True TODO resnet_v2: "return logits, end_points" instead of "return net, end_points" #with slim.arg_scope(resnet_v2.resnet_arg_scope()): # logits, _ = resnet_v2.resnet_v2_50(images,num_classes, is_training=True) # TODO Choose Model (50, 101, 152, ...) #TODO add no classes (num_classes=dataset.num_classes,) => features before logit layer gets returned => add own layers afterwards with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=0.0002)): logits, _ = resnet_v2.resnet_v2_50(images, is_training=is_training) #TODO logits = tf.nn.dropout(logits, 0.5) # MAXOUT max_out_unit_1 = slim.fully_connected(logits, 512, activation_fn=None, normalizer_fn=None, scope='my_fc_2') max_out_unit_2 = slim.fully_connected(logits, 512, activation_fn=None, normalizer_fn=None, scope='my_fc_3') max_out_unit_3 = slim.fully_connected(logits, 512, activation_fn=None, normalizer_fn=None, scope='my_fc_4') max_out_unit_4 = slim.fully_connected(logits, 512, activation_fn=None, normalizer_fn=None, scope='my_fc_5') max_out_joined = tf.concat([max_out_unit_1, max_out_unit_2, max_out_unit_3, max_out_unit_4], 1) logits = my_functions.max_out(max_out_joined, num_units = 1, axis = 1) #logits = tf.nn.dropout(logits, 0.5) #TODO needed? "Dropout with a ratio of 50% is applieder after the maxout layer and before the classifier" (paper), "Dropout is performed on x, before multiplication by weights" (presentation) # Fully Connected. 1000 neurons. logits = tf.squeeze(logits, [1, 2], name='SpatialSqueeze') logits = slim.fully_connected(logits, 1000, scope = "my_fc_1", activation_fn=None, normalizer_fn=None) if is_training: return logits elif dropout_rate == 1.0: return logits else: return slim.softmax(logits)
def graph(x, y, i, x_max, x_min, grad, eps_inside): num_iter = FLAGS.num_iter alpha = eps_inside / num_iter momentum = FLAGS.momentum num_classes = 1001 with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_v3, end_points_v3 = inception_v3.inception_v3( x, num_classes=num_classes, is_training=False, scope='Ens3AdvInceptionV3') with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_res_v2, end_points_res_v2 = inception_resnet_v2.inception_resnet_v2( x, num_classes=num_classes, is_training=False) with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits_resnet, end_points_resnet = resnet_v2.resnet_v2_50( x, num_classes=num_classes, is_training=False) pred = tf.argmax( end_points_v3['Predictions'] + end_points_res_v2['Predictions'] + end_points_resnet['predictions'], 1) first_round = tf.cast(tf.equal(i, 0), tf.int64) y = first_round * pred + (1 - first_round) * y one_hot = tf.one_hot(y, num_classes) logits = (logits_v3 + logits_res_v2 + logits_resnet) / 3 auxlogits = (end_points_v3['AuxLogits'] + end_points_res_v2['AuxLogits']) / 2 cross_entropy = tf.losses.softmax_cross_entropy(one_hot, logits, label_smoothing=0.0, weights=1.0) cross_entropy += tf.losses.softmax_cross_entropy(one_hot, auxlogits, label_smoothing=0.0, weights=0.4) noise = tf.gradients(cross_entropy, x)[0] x = x + alpha * tf.sign(noise) x = tf.clip_by_value(x, x_min, x_max) i = tf.add(i, 1) return x, y, i, x_max, x_min, noise, eps_inside
def __call__(self, image_input, reuse=False, training=False, keep_prob=1.0, endpoint_name='Mixed_7d'): weight_decay = FLAGS.weight_decay activation_fn = tf.nn.relu end_points = {} with slim.arg_scope( resnet_v2.resnet_arg_scope(weight_decay=weight_decay)): with tf.variable_scope("", reuse=reuse): net, end_points = resnet_v2.resnet_v2_50(image_input, is_training=training, global_pool=False) print(net.shape) return net
def eval(): # This might take a few minutes. with tf.Graph().as_default(): #tf.logging.set_verbosity(tf.logging.INFO) dataset = flowers.get_split( 'train', flowers_data_dir ) # TODO Add direcotry of dataset, Check for format of dataset! images, images_raw, labels = load_batch( dataset, height=image_size, width=image_size ) # TODO load_batch really necessary? Processing all! # Create the model, use the default arg scope to configure the batch norm parameters. with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, _ = resnet_v2.resnet_v2_50( images, num_classes=dataset.num_classes, is_training=True) # TODO Choose Model (50, 101, 152, ...) predictions = tf.argmax(logits, 1) checkpoint_path = tf.train.latest_checkpoint(train_dir) init_fn = slim.assign_from_checkpoint_fn( checkpoint_path, slim.get_variables_to_restore()) # Define the metrics: names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({ 'eval/Accuracy': slim.metrics.streaming_accuracy(predictions, labels), 'eval/Recall@5': slim.metrics.streaming_sparse_recall_at_k(logits, labels, 5) }) print('Running evaluation Loop...') checkpoint_path = tf.train.latest_checkpoint(train_dir) metric_values = slim.evaluation.evaluate_once( master='', checkpoint_path=checkpoint_path, logdir=train_dir, eval_op=list(names_to_updates.values()), final_op=list(names_to_values.values())) names_to_values = dict(zip(names_to_values.keys(), metric_values)) for name in names_to_values: print('%s: %f' % (name, names_to_values[name]))
def basic_model(inputs, num_classes, is_training=True, is_reuse=tf.compat.v1.AUTO_REUSE, keep_prob=0.8, attention_module=None, scope='basic_model'): ''' :param inputs: N x H x W x C tensor :return: ''' with slim.arg_scope(resnet_v2.resnet_arg_scope()): net, end_points = \ resnet_v2.resnet_v2_50(inputs, num_classes=num_classes, is_training=is_training, reuse=is_reuse, attention_module=attention_module, scope='resnet_v2_50') # # Global average pooling. # net = tf.reduce_mean(net, [1, 2], name='pool5', keep_dims=True) # end_points['global_pool'] = net # # batch_norm_params['is_training'] = is_training # # net = slim.batch_norm(net, scope='batch_norm') # # end_points['batch_norm'] = net # net = slim.flatten(net, scope='flatten') # end_points['flatten'] = net # net = slim.fully_connected(net, 256, normalizer_fn=slim.batch_norm, # normalizer_params=batch_norm_params, scope='fc1') # end_points['fc1'] = net # # net = slim.fully_connected(net, num_classes, normalizer_fn=slim.batch_norm, # normalizer_params=batch_norm_params, activation_fn=None, scope='fc2') # end_points['fc2'] = net logits = net return logits, end_points
def get_embeddings(instances, model_name, return_dict): image_size = 299 query_embeddings = [] os.environ['CUDA_VISIBLE_DEVICES'] = '0' with tf.Graph().as_default(): image = tf.placeholder(tf.uint8, (None, None, 3)) processed_image = inception_preprocessing.preprocess_image( image, image_size, image_size, is_training=False) processed_image = tf.expand_dims(processed_image, 0) with slim.arg_scope(resnet_v2.resnet_arg_scope()): if model_name == 'resnet_v2_101': logits, _ = resnet_v2.resnet_v2_101(processed_image, 1001, is_training=False) pool5 = tf.get_default_graph().get_tensor_by_name( "resnet_v2_101/pool5:0") elif model_name == 'resnet_v2_50': logits, _ = resnet_v2.resnet_v2_50(processed_image, 1001, is_training=False) pool5 = tf.get_default_graph().get_tensor_by_name( "resnet_v2_50/pool5:0") else: print("Unknown model") exit(0) if model_name == 'resnet_v2_101': init_fn = slim.assign_from_checkpoint_fn( 'resnet_v2_101.ckpt', slim.get_model_variables('resnet_v2')) elif model_name == 'resnet_v2_50': init_fn = slim.assign_from_checkpoint_fn( 'resnet_v2_50.ckpt', slim.get_model_variables('resnet_v2')) with tf.Session() as sess: init_fn(sess) for ins, patch, vis_img in instances: scaled_img, logit_vals, embedding = sess.run( [processed_image, logits, pool5], feed_dict={image: patch}) query_embeddings.append((ins, patch, vis_img, embedding[0, 0, 0, :])) return_dict['query_embeddings'] = query_embeddings
def basic(inputs, num_classes, is_training=True, dropout_keep_prob=0.8, reuse=tf.compat.v1.AUTO_REUSE): ''' Args: inputs: N x V x H x W x C tensor scope: ''' final_view_descriptors = [] n_views = inputs.get_shape().as_list()[1] # transpose views: (NxVxHxWxC) -> (VxNxHxWxC) views = tf.transpose(inputs, perm=[1, 0, 2, 3, 4]) for index in range(n_views): batch_view = tf.gather(views, index) # N x H x W x C # with slim.arg_scope(inception_v3.inception_v3_arg_scope()): # logits, end_points = inception_v3.inception_v3(batch_view, # num_classes = num_classes, # is_training=is_training, # dropout_keep_prob=dropout_keep_prob, # reuse=reuse) # final_view_descriptors.append(end_points['Mixed_7c']) with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, end_points = resnet_v2.resnet_v2_50(batch_view, num_classes=num_classes, is_training=is_training, reuse=reuse) final_view_descriptors.append(end_points['resnet_v2_50/block4']) shape_descriptor = tf.reduce_max(final_view_descriptors, axis=0) net = tf.keras.layers.GlobalAveragePooling2D()(shape_descriptor) logits = tf.keras.layers.Dense(num_classes)(net) return shape_descriptor, logits
def getLoss(x_div224, target_class_input, raw_class_input): red, green, blue = tf.split(axis=3, num_or_size_splits=3, value=x_div224) x_224 = x_div224 x_div224 = tf.concat(axis=3, values=[blue, green, red]) x_div224 = tf.transpose(x_div224, [0, 3, 1, 2]) #在这里,scope的名字作为checkpoint前缀 with tf.variable_scope("R152_Denoise"): with f1('R152_Denoise', is_training=False): logits1 = f2(x_div224) with tf.variable_scope("X101_Denoise"): with f1('X101_Denoise', is_training=False): logits2 = f3(x_div224) with tf.variable_scope("R152"): with f1('R152', is_training=False): logits3 = f4(x_div224) with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits4, _ = resnet_v2.resnet_v2_50(x_224, 1001, is_training=False, scope='resnet_v2_50', reuse=tf.AUTO_REUSE) one_hot_target_class = tf.one_hot(target_class_input - 1, num_classes - 1) one_hot_target_class2 = tf.one_hot(target_class_input, num_classes) cross_entropy = tf.losses.softmax_cross_entropy( one_hot_target_class, (logits1 + logits2 + logits3) / 3, label_smoothing=0.0, weights=1.0) cross_entropy += tf.losses.softmax_cross_entropy(one_hot_target_class2, logits4, label_smoothing=0.0, weights=0.3) return cross_entropy
def _construct_model(model_type='resnet_v1_50'): """Constructs model for the desired type of CNN. Args: model_type: Type of model to be used. Returns: end_points: A dictionary from components of the network to the corresponding activations. Raises: ValueError: If the model_type is not supported. """ # Placeholder input. images = array_ops.placeholder( dtypes.float32, shape=(1, None, None, 3), name=_INPUT_NODE) # Construct model. if model_type == 'inception_resnet_v2': _, end_points = inception.inception_resnet_v2_base(images) elif model_type == 'inception_resnet_v2-same': _, end_points = inception.inception_resnet_v2_base( images, align_feature_maps=True) elif model_type == 'inception_v2': _, end_points = inception.inception_v2_base(images) elif model_type == 'inception_v2-no-separable-conv': _, end_points = inception.inception_v2_base( images, use_separable_conv=False) elif model_type == 'inception_v3': _, end_points = inception.inception_v3_base(images) elif model_type == 'inception_v4': _, end_points = inception.inception_v4_base(images) elif model_type == 'alexnet_v2': _, end_points = alexnet.alexnet_v2(images) elif model_type == 'vgg_a': _, end_points = vgg.vgg_a(images) elif model_type == 'vgg_16': _, end_points = vgg.vgg_16(images) elif model_type == 'mobilenet_v1': _, end_points = mobilenet_v1.mobilenet_v1_base(images) elif model_type == 'mobilenet_v1_075': _, end_points = mobilenet_v1.mobilenet_v1_base( images, depth_multiplier=0.75) elif model_type == 'resnet_v1_50': _, end_points = resnet_v1.resnet_v1_50( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v1_101': _, end_points = resnet_v1.resnet_v1_101( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v1_152': _, end_points = resnet_v1.resnet_v1_152( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v1_200': _, end_points = resnet_v1.resnet_v1_200( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_50': _, end_points = resnet_v2.resnet_v2_50( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_101': _, end_points = resnet_v2.resnet_v2_101( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_152': _, end_points = resnet_v2.resnet_v2_152( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_200': _, end_points = resnet_v2.resnet_v2_200( images, num_classes=None, is_training=False, global_pool=False) else: raise ValueError('Unsupported model_type %s.' % model_type) return end_points
def train(name): with tf.Graph().as_default() as graph: if name == 'coat_length_labels': train_file_path = tf.gfile.Glob(os.path.join("./data/tfrecords/train/coat_length_labels/", '*.tfrecord')) save_file_path = "./model/coat_length_labels/" X = tf.placeholder(tf.float32, [None, 224, 224, 3], name='x-input') Y = tf.placeholder(tf.float32, [None, 8], name='y-input') num = 8 elif name == 'collar_design_labels': train_file_path = tf.gfile.Glob(os.path.join("./data/tfrecords/train/collar_design_labels/", '*.tfrecord')) save_file_path = "./model/collar_design_labels/" X = tf.placeholder(tf.float32, [None, 224, 224, 3], name='x-input') Y = tf.placeholder(tf.float32, [None, 5], name='y-input') num = 5 elif name == 'lapel_design_labels': train_file_path = tf.gfile.Glob(os.path.join("./data/tfrecords/train/lapel_design_labels/", '*.tfrecord')) save_file_path = "./model/lapel_design_labels/" X = tf.placeholder(tf.float32, [None, 224, 224, 3], name='x-input') Y = tf.placeholder(tf.float32, [None, 5], name='y-input') num = 5 elif name == 'neck_design_labels': train_file_path = tf.gfile.Glob(os.path.join("./data/tfrecords/train/neck_design_labels/", '*.tfrecord')) save_file_path = "./model/neck_design_labels/" X = tf.placeholder(tf.float32, [None, 224, 224, 3], name='x-input') Y = tf.placeholder(tf.float32, [None, 5], name='y-input') num = 5 elif name == 'neckline_design_labels': train_file_path = tf.gfile.Glob(os.path.join("./data/tfrecords/train/neckline_design_labels/", '*.tfrecord')) save_file_path = "./model/neckline_design_labels/" X = tf.placeholder(tf.float32, [None, 224, 224, 3], name='x-input') Y = tf.placeholder(tf.float32, [None, 10], name='y-input') num = 10 elif name == 'pant_length_labels': train_file_path = tf.gfile.Glob(os.path.join("./data/tfrecords/train/pant_length_labels/", '*.tfrecord')) save_file_path = "./model/pant_length_labels/" X = tf.placeholder(tf.float32, [None, 224, 224, 3], name='x-input') Y = tf.placeholder(tf.float32, [None, 6], name='y-input') num = 6 elif name == 'skirt_length_labels': train_file_path = tf.gfile.Glob(os.path.join("./data/tfrecords/train/skirt_length_labels/", '*.tfrecord')) save_file_path = "./model/skirt_length_labels/" X = tf.placeholder(tf.float32, [None, 224, 224, 3], name='x-input') Y = tf.placeholder(tf.float32, [None, 6], name='y-input') num = 6 elif name == 'sleeve_length_labels': train_file_path = tf.gfile.Glob(os.path.join("./data/tfrecords/train/sleeve_length_labels/", '*.tfrecord')) save_file_path = "./model/sleeve_length_labels/" X = tf.placeholder(tf.float32, [None, 224, 224, 3], name='x-input') Y = tf.placeholder(tf.float32, [None, 9], name='y-input') num = 9 with slim.arg_scope(resnet_v2.resnet_arg_scope()): trX_, trY_ = get_data_train(name, train_file_path, 512) net, end_points = resnet_v2.resnet_v2_50(X, is_training=False, global_pool=False) net = end_points['resnet_v2_50/block4/unit_1/bottleneck_v2/conv1'] net = conv2d_bn(net, 512, [3, 3], variables_collections=['CusW'], scope='out1') net = conv2d_bn(net, 512, [3, 3], variables_collections=['CusW'], scope='out2') net = conv2d_bn(net, 512, [3, 3], variables_collections=['CusW'], scope='out3') net = slim.flatten(net) net = slim.fully_connected(net, 256, activation_fn=tf.nn.relu, variables_collections=['CusW'], scope='out4') net = slim.fully_connected(net, num, activation_fn=tf.nn.sigmoid, variables_collections=['CusW'], scope='out5') cost = loss_func(net, Y) var_list = [*tf.get_collection('CusW')] print(net) optimizer = tf.train.AdamOptimizer() update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies([tf.group(*update_ops)]): train_op = optimizer.minimize(cost, var_list=var_list) with tf.Session(graph=graph) as sess: tf.global_variables_initializer().run() tf.local_variables_initializer().run() print("恢复模型:") model_path = './resnet_v2_50_2017_04_14/resnet_v2_50.ckpt' variables_to_restore = slim.get_variables_to_restore( exclude=['out1', 'out2', 'out3', 'out4', 'out5'] ) init_fn = slim.assign_from_checkpoint_fn( model_path, variables_to_restore, ignore_missing_vars=True) init_fn(sess) print("模型恢复成功") coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) for i in range(1, 1001): trX, trY = sess.run([trX_, trY_]) training_batch = zip(range(0, len(trX), 64), range(64, len(trX)+1, 64)) for start, end in training_batch: cost_train, train_op_ = sess.run( [cost, train_op], feed_dict={X: trX[start:end], Y: trY[start:end]}) if i == AI[name]: new_graph = graph_util.convert_variables_to_constants(sess, sess.graph_def, output_node_names=['out4/Sigmoid']) tf.train.write_graph(new_graph, save_file_path, 'graph.pb', as_text=False) break coord.request_stop() coord.join(threads)
def main(margin, batch_size, output_size, learning_rate, whichGPU, is_finetuning, pretrained_net): def handler(signum, frame): print 'Saving checkpoint before closing' pretrained_net = os.path.join(ckpt_dir, 'checkpoint-' + param_str) saver.save(sess, pretrained_net, global_step=step) print 'Checkpoint-', pretrained_net + '-' + str(step), ' saved!' sys.exit(0) signal.signal(signal.SIGINT, handler) ckpt_dir = './output/expedia/ckpts' log_dir = './output/expedia/logs' train_filename = './input/expedia_train_by_hotel.txt' mean_file = './input/meanIm.npy' img_size = [256, 256] crop_size = [224, 224] num_iters = 200000 summary_iters = 100 save_iters = 5000 featLayer = 'resnet_v2_50/logits' is_training = True margin = float(margin) batch_size = int(batch_size) output_size = int(output_size) learning_rate = float(learning_rate) whichGPU = str(whichGPU) if batch_size % 30 != 0: print 'Batch size must be divisible by 30!' sys.exit(0) num_pos_examples = batch_size / 30 # Create data "batcher" train_data = CombinatorialTripletSet(train_filename, mean_file, img_size, crop_size, batch_size, num_pos_examples, isTraining=is_training) numClasses = len(train_data.hotels.keys()) numIms = np.sum( [len(train_data.hotels[h]['ims']) for h in train_data.hotels.keys()]) datestr = datetime.now().strftime("%Y_%m_%d_%H%M") param_str = datestr + '_lr' + str(learning_rate).replace( '.', 'pt') + '_outputSz' + str(output_size) + '_margin' + str( margin).replace('.', 'pt') logfile_path = os.path.join(log_dir, param_str + '_train.txt') train_log_file = open(logfile_path, 'a') print '------------' print '' print 'Going to train with the following parameters:' print '# Classes: ', numClasses train_log_file.write('# Classes: ' + str(numClasses) + '\n') print '# Ims: ', numIms train_log_file.write('# Ims: ' + str(numIms) + '\n') print 'Margin: ', margin train_log_file.write('Margin: ' + str(margin) + '\n') print 'Output size: ', output_size train_log_file.write('Output size: ' + str(output_size) + '\n') print 'Learning rate: ', learning_rate train_log_file.write('Learning rate: ' + str(learning_rate) + '\n') print 'Logging to: ', logfile_path train_log_file.write('Param_str: ' + param_str + '\n') train_log_file.write('----------------\n') print '' print '------------' # Queuing op loads data into input tensor image_batch = tf.placeholder( tf.float32, shape=[batch_size, crop_size[0], crop_size[0], 3]) noise = tf.random_normal(shape=[batch_size, crop_size[0], crop_size[0], 1], mean=0.0, stddev=0.0025, dtype=tf.float32) final_batch = tf.add(image_batch, noise) print("Preparing network...") with slim.arg_scope(resnet_v2.resnet_arg_scope()): _, layers = resnet_v2.resnet_v2_50(final_batch, num_classes=output_size, is_training=True) variables_to_restore = [] for var in slim.get_model_variables(): excluded = False if is_finetuning.lower() == 'true' and var.op.name.startswith( 'resnet_v2_50/logits') or 'momentum' in var.op.name.lower(): excluded = True if not excluded: variables_to_restore.append(var) # feat = tf.squeeze(tf.nn.l2_normalize(layers[featLayer],3)) feat = tf.squeeze(layers[featLayer]) expanded_a = tf.expand_dims(feat, 1) expanded_b = tf.expand_dims(feat, 0) D = tf.reduce_sum(tf.squared_difference(expanded_a, expanded_b), 2) # D = 1 - tf.reduce_sum(tf.multiply(expanded_a, expanded_b), 2) # if not train_data.isOverfitting: # D_max = tf.reduce_max(D) # D_mean, D_var = tf.nn.moments(D, axes=[0,1]) # lowest_nonzero_distance = tf.reduce_max(-D) # bottom_thresh = 1.2*lowest_nonzero_distance # top_thresh = (D_max + D_mean)/2.0 # bool_mask = tf.logical_and(D>=bottom_thresh,D<=top_thresh) # D = tf.multiply(D,tf.cast(bool_mask,tf.float32)) posIdx = np.floor(np.arange(0, batch_size) / num_pos_examples).astype('int') posIdx10 = num_pos_examples * posIdx posImInds = np.tile(posIdx10, (num_pos_examples, 1)).transpose() + np.tile( np.arange(0, num_pos_examples), (batch_size, 1)) anchorInds = np.tile(np.arange(0, batch_size), (num_pos_examples, 1)).transpose() posImInds_flat = posImInds.ravel() anchorInds_flat = anchorInds.ravel() posPairInds = zip(posImInds_flat, anchorInds_flat) posDists = tf.reshape(tf.gather_nd(D, posPairInds), (batch_size, num_pos_examples)) shiftPosDists = tf.reshape(posDists, (1, batch_size, num_pos_examples)) posDistsRep = tf.tile(shiftPosDists, (batch_size, 1, 1)) allDists = tf.tile(tf.expand_dims(D, 2), (1, 1, num_pos_examples)) ra, rb, rc = np.meshgrid(np.arange(0, batch_size), np.arange(0, batch_size), np.arange(0, num_pos_examples)) bad_negatives = np.floor((ra) / num_pos_examples) == np.floor( (rb) / num_pos_examples) bad_positives = np.mod(rb, num_pos_examples) == np.mod(rc, num_pos_examples) mask = ((1 - bad_negatives) * (1 - bad_positives)).astype('float32') # loss = tf.reduce_sum(tf.maximum(0.,tf.multiply(mask,margin + posDistsRep - allDists)))/batch_size loss = tf.reduce_mean( tf.maximum(0., tf.multiply(mask, margin + posDistsRep - allDists))) # slightly counterintuitive to not define "init_op" first, but tf vars aren't known until added to graph update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): # train_op = tf.train.AdamOptimizer(learning_rate).minimize(loss) optimizer = tf.train.AdamOptimizer(learning_rate) train_op = slim.learning.create_train_op(loss, optimizer) summary_op = tf.summary.merge_all() init_op = tf.global_variables_initializer() # Create a saver for writing training checkpoints. saver = tf.train.Saver(max_to_keep=2000) # tf will consume any GPU it finds on the system. Following lines restrict it to specific gpus c = tf.ConfigProto() c.gpu_options.visible_device_list = whichGPU print("Starting session...") sess = tf.Session(config=c) sess.run(init_op) writer = tf.summary.FileWriter(log_dir, sess.graph) restore_fn = slim.assign_from_checkpoint_fn(pretrained_net, variables_to_restore) restore_fn(sess) print("Start training...") ctr = 0 for step in range(num_iters): start_time = time.time() batch, labels, ims = train_data.getBatch() _, loss_val = sess.run([train_op, loss], feed_dict={image_batch: batch}) end_time = time.time() duration = end_time - start_time out_str = 'Step %d: loss = %.6f -- (%.3f sec)' % (step, loss_val, duration) # print(out_str) if step % summary_iters == 0: print(out_str) train_log_file.write(out_str + '\n') # Update the events file. # summary_str = sess.run(summary_op) # writer.add_summary(summary_str, step) # writer.flush() # # Save a checkpoint if (step + 1) % save_iters == 0: print('Saving checkpoint at iteration: %d' % (step)) pretrained_net = os.path.join(ckpt_dir, 'checkpoint-' + param_str) saver.save(sess, pretrained_net, global_step=step) print 'checkpoint-', pretrained_net + '-' + str(step), ' saved!' if (step + 1) == num_iters: print('Saving final') pretrained_net = os.path.join(ckpt_dir, 'final-' + param_str) saver.save(sess, pretrained_net, global_step=step) print 'final-', pretrained_net + '-' + str(step), ' saved!' sess.close() train_log_file.close()