def run_model(cluster, config_proto): with wh.replica(devices=cluster.slices[0]): iterator = get_mock_iterator() images, labels = iterator.get_next() feature_extract_fn = resnet_model.imagenet_resnet_v2(50) features = feature_extract_fn(images, is_training=True) with wh.split(devices=cluster.slices[0]): logits = tf.layers.dense(features, class_num) loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits) global_step = tf.train.get_or_create_global_step() optimizer = tf.train.AdamOptimizer(learning_rate=0.9) train_op = optimizer.minimize(loss, global_step=global_step) hooks = [] hooks = [tf.train.StopAtStepHook(last_step=200)] with tf.train.MonitoredTrainingSession(config=config_proto, hooks=hooks) as sess: while not sess.should_stop(): starttime = time.time() train_loss, _, step = sess.run([loss, train_op, global_step]) endtime = time.time() print("[Iteration {} ], Loss: {:.6} , Time: {:.4} ." \ .format(step, train_loss, endtime-starttime)) print("[Finished]")
def __init__(self, sess, config, name, is_train): self.sess = sess self.name = name self.is_train = is_train # image shape for grayscale images im_shape = [config.batch_size] + config.im_size + [1] #[1, 222, 247, 1] # x => moving image, y => fixed image self.x = tf.placeholder(tf.float32, im_shape) self.y = tf.placeholder(tf.float32, im_shape) self.labels = tf.placeholder(tf.int32, [config.batch_size]) # x and y concatenated in color channel self.xy = tf.concat([self.x, self.y], 3) self.model = imagenet_resnet_v2(18, 5, data_format='channels_first') self.logits = self.model(self.x, is_training=True) # create predictions => filter highest likelyhood from logits self.prediction = tf.argmax(self.logits, 1) self.var_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) self.saver = tf.train.Saver(self.var_list) if self.is_train: # loss definition and weighting of the 2 loss functions self.loss = - self.disease_loss(self.labels, self.logits) # self.loss = mse(self.y, self.z) self.optim = tf.train.AdamOptimizer(config.lr) self.train = self.optim.minimize( - self.loss) # self.sess.run(tf.variables_initializer(self.vCNN.var_list)) self.sess.run(tf.global_variables_initializer())
def tensor_shapes_helper(self, resnet_size, with_gpu=False): """Checks the tensor shapes after each phase of the ResNet model.""" def reshape(shape): """Returns the expected dimensions depending on if a GPU is being used.""" # If a GPU is used for the test, the shape is returned (already in NCHW # form). When GPU is not used, the shape is converted to NHWC. if with_gpu: return shape return shape[0], shape[2], shape[3], shape[1] graph = tf.Graph() with graph.as_default(), self.test_session(use_gpu=with_gpu, force_gpu=with_gpu): model = resnet_model.imagenet_resnet_v2( resnet_size, 456, data_format='channels_first' if with_gpu else 'channels_last') inputs = tf.random_uniform([1, 224, 224, 3]) output = model(inputs, is_training=True) initial_conv = graph.get_tensor_by_name('initial_conv:0') max_pool = graph.get_tensor_by_name('initial_max_pool:0') block_layer1 = graph.get_tensor_by_name('block_layer1:0') block_layer2 = graph.get_tensor_by_name('block_layer2:0') block_layer3 = graph.get_tensor_by_name('block_layer3:0') block_layer4 = graph.get_tensor_by_name('block_layer4:0') avg_pool = graph.get_tensor_by_name('final_avg_pool:0') dense = graph.get_tensor_by_name('final_dense:0') self.assertAllEqual(initial_conv.shape, reshape((1, 64, 112, 112))) self.assertAllEqual(max_pool.shape, reshape((1, 64, 56, 56))) # The number of channels after each block depends on whether we're # using the building_block or the bottleneck_block. if resnet_size < 50: self.assertAllEqual(block_layer1.shape, reshape( (1, 64, 56, 56))) self.assertAllEqual(block_layer2.shape, reshape((1, 128, 28, 28))) self.assertAllEqual(block_layer3.shape, reshape((1, 256, 14, 14))) self.assertAllEqual(block_layer4.shape, reshape( (1, 512, 7, 7))) self.assertAllEqual(avg_pool.shape, reshape((1, 512, 1, 1))) else: self.assertAllEqual(block_layer1.shape, reshape((1, 256, 56, 56))) self.assertAllEqual(block_layer2.shape, reshape((1, 512, 28, 28))) self.assertAllEqual(block_layer3.shape, reshape((1, 1024, 14, 14))) self.assertAllEqual(block_layer4.shape, reshape( (1, 2048, 7, 7))) self.assertAllEqual(avg_pool.shape, reshape((1, 2048, 1, 1))) self.assertAllEqual(dense.shape, (1, 456)) self.assertAllEqual(output.shape, (1, 456))
def resnet_model_fn(inputs, training): """Our model_fn for ResNet to be used with our Estimator.""" network = resnet_model.imagenet_resnet_v2(resnet_size=18, num_classes=class_num, mode='se', data_format=None) inputs = network(inputs=inputs, is_training=training) feat = tf.nn.l2_normalize(inputs, 1, 1e-10, name='feat') inputs = tf.layers.dense(inputs=inputs, units=class_num) # inputs = tf.layers.dense(inputs=feat, units=class_num) inputs = tf.identity(inputs, 'final_dense') return inputs, feat
def tensor_shapes_helper(self, resnet_size, with_gpu=False): """Checks the tensor shapes after each phase of the ResNet model.""" def reshape(shape): """Returns the expected dimensions depending on if a GPU is being used.""" # If a GPU is used for the test, the shape is returned (already in NCHW # form). When GPU is not used, the shape is converted to NHWC. if with_gpu: return shape return shape[0], shape[2], shape[3], shape[1] graph = tf.Graph() with graph.as_default(), self.test_session( use_gpu=with_gpu, force_gpu=with_gpu): model = resnet_model.imagenet_resnet_v2( resnet_size, 456, data_format='channels_first' if with_gpu else 'channels_last') inputs = tf.random_uniform([1, 224, 224, 3]) output = model(inputs, is_training=True) initial_conv = graph.get_tensor_by_name('initial_conv:0') max_pool = graph.get_tensor_by_name('initial_max_pool:0') block_layer1 = graph.get_tensor_by_name('block_layer1:0') block_layer2 = graph.get_tensor_by_name('block_layer2:0') block_layer3 = graph.get_tensor_by_name('block_layer3:0') block_layer4 = graph.get_tensor_by_name('block_layer4:0') avg_pool = graph.get_tensor_by_name('final_avg_pool:0') dense = graph.get_tensor_by_name('final_dense:0') self.assertAllEqual(initial_conv.shape, reshape((1, 64, 112, 112))) self.assertAllEqual(max_pool.shape, reshape((1, 64, 56, 56))) # The number of channels after each block depends on whether we're # using the building_block or the bottleneck_block. if resnet_size < 50: self.assertAllEqual(block_layer1.shape, reshape((1, 64, 56, 56))) self.assertAllEqual(block_layer2.shape, reshape((1, 128, 28, 28))) self.assertAllEqual(block_layer3.shape, reshape((1, 256, 14, 14))) self.assertAllEqual(block_layer4.shape, reshape((1, 512, 7, 7))) self.assertAllEqual(avg_pool.shape, reshape((1, 512, 1, 1))) else: self.assertAllEqual(block_layer1.shape, reshape((1, 256, 56, 56))) self.assertAllEqual(block_layer2.shape, reshape((1, 512, 28, 28))) self.assertAllEqual(block_layer3.shape, reshape((1, 1024, 14, 14))) self.assertAllEqual(block_layer4.shape, reshape((1, 2048, 7, 7))) self.assertAllEqual(avg_pool.shape, reshape((1, 2048, 1, 1))) self.assertAllEqual(dense.shape, (1, 456)) self.assertAllEqual(output.shape, (1, 456))
def serving_input_to_output(features, mode, k=TOP_K): '''End-to-end function from serving input to output.''' # Preprocess inputs before sending tensors to the network. processed_images = preprocess_input(features) # Build network graph and connect to processed_images network = imagenet_resnet_v2(RESNET_SIZE, _LABEL_CLASSES, data_format='channels_last') logits = network(inputs=processed_images, is_training=(mode == tf.estimator.ModeKeys.TRAIN)) # Postprocess network output (logits) and return top k predictions. predictions = postprocess_output(logits, k=k) return predictions
def main(): params = { 'resnet_size': resnet_size, 'data_format': data_format, 'batch_size': BatchSize, } features = tf.placeholder(tf.float32, [1, 227, 227, 3]) features_crop = features[:, 0:224, 0:224, :] / 255.0 network = alexnet_model.alexnet_memory_generator( keep_prob=0.5, data_format='channels_last') conv5_1, models = network(inputs=features, is_training=False) network2 = resnet_model.imagenet_resnet_v2( params['resnet_size'], 71, data_format=params['data_format']) models2 = network2(inputs=features_crop, is_training=False) concate = tf.concat([models, models2], axis=1) results1 = alexnet_model.fc(concate, 6144, 4096, name='fc-euclidean-1') results = alexnet_model.fc(results1, 4096, 1, relu=False, name='fc-euclidean-2') img_tensor = tf.placeholder(tf.float64, [256, 256, 3]) img_preprocessed = _parse_function_single(img_tensor) with tf.Session() as sess: saver = tf.train.Saver(max_to_keep=None) saver.restore(sess, "./Models/model.ckpt") pic_name = args.img img = cv2.imread(pic_name) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.resize(img, (256, 256)) img = img / 255 img_input = sess.run([img_preprocessed], feed_dict={img_tensor: img}) my_pred = sess.run([results], feed_dict={features: img_input}) score = list(my_pred[0]) score = score[0][0] print('The memorability score of {}: {}'.format(pic_name, score))
def create_resnet_model(fingerprint_input, model_settings, is_training): label_count = model_settings['label_count'] input_frequency_size = model_settings['dct_coefficient_count'] input_time_size = model_settings['spectrogram_length'] if is_training: dropout_prob = tf.placeholder(tf.float32, name='dropout_prob') fingerprint_4d = tf.reshape(fingerprint_input, [-1, input_time_size, input_frequency_size, 1]) network = resnet_model.imagenet_resnet_v2(50, label_count, "channels_last") logits = network(inputs=fingerprint_4d, is_training=is_training) #logits = Dense(label_count)(logits) tf.logging.info("resnet logits dimension " + str(logits.get_shape().as_list())) if is_training: return logits, dropout_prob else: return logits
def _tower_fn(is_training, weight_decay, feature, label, data_format, resnet_size, batch_norm_decay, batch_norm_epsilon): """Build computation tower (Resnet). Args: is_training: true if is training graph. weight_decay: weight regularization strength, a float. feature: a Tensor. label: a Tensor. data_format: channels_last (NHWC) or channels_first (NCHW). num_layers: number of layers, an int. batch_norm_decay: decay for batch normalization, a float. batch_norm_epsilon: epsilon for batch normalization, a float. Returns: A tuple with the loss for the tower, the gradients and parameters, and predictions. """ network = resnet_model.imagenet_resnet_v2(resnet_size, 1000 + 1, data_format) logits = network(feature, is_training=is_training) _, top_k_indices = tf.nn.top_k(logits, k=5) tower_pred = { 'classes': tf.argmax(input=logits, axis=1), 'probabilities': tf.nn.softmax(logits), 'top5_classes': top_k_indices } tower_loss = tf.compat.v1.losses.sparse_softmax_cross_entropy( logits=logits, labels=label) tower_loss = tf.reduce_mean(tower_loss) model_params = tf.compat.v1.trainable_variables() tower_loss += weight_decay * tf.add_n( [tf.nn.l2_loss(v) for v in model_params]) #tower_grad = tf.gradients(tower_loss, model_params) return tower_loss, tower_pred
def resnet_model_fn(features, labels, mode, params): network = resnet_model.imagenet_resnet_v2(params['resnet_size'], _LABEL_CLASSED, params['data_format']) logits = network(inputs=features, is_training=(mode == tf.estimator.ModeKeys.TRAIN)) predictions = { 'classes': tf.argmax(logits, axis=1), 'probabilities': tf.nn.softmax(logits, name='softmax_tensor') } if mode == tf.estimator.ModeKeys.PREDICT: return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions) cross_entropy = tf.losses.softmax_cross_entropy(onehot_labels=labels, logits=logits) tf.identity(cross_entropy, name='cross_entropy') tf.summary.scalar('cross_entropy', cross_entropy) loss = cross_entropy + _WEIGHT_DECAY * tf.add_n([ tf.nn.l2_loss(v) for v in tf.trainable_variables() if 'batch_normalization' not in v.name ]) if mode == tf.estimator.ModeKeys.TRAIN: # Scale the learning rate linearly with the batch size. # when the batch size is 256, then learning rate should be 0.1 initial_leaning_rate = 0.1 * params['batch_size'] / 256 batches_per_epoch = _NUM_IMAGES['train'] / params['batch_size'] global_step = tf.train.get_or_create_global_step() boundaries = [ int(batches_per_epoch * epoch) for epoch in [30, 60, 80, 90] ] values = [ initial_leaning_rate * decay for decay in [1, 0.1, 00.1, 1e-3, 1e-4] ] leaning_rate = tf.train.piecewise_constant( tf.cast(global_step, tf.int32), boundaries, values) tf.identity(leaning_rate, name='learning_rate') tf.summary.scalar('learning_rate', leaning_rate) optimizer = tf.train.MomentumOptimizer(learning_rate=leaning_rate, momentum=_MOMENTUN) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.convert_to_tensor(update_ops): train_op = optimizer.minimize(logits, global_step) else: train_op = None accuracy = tf.metrics.accuracy(tf.argmax(labels, axis=1), predictions['classes']) metrics = {'accuracy': accuracy} tf.identity(accuracy[1], name="train_accuracy") tf.summary.scalar('train_accuracy', accuracy[1]) return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions, loss=loss, train_op=train_op, eval_metric_ops=metrics)
def resnet_model_fn(features, labels, mode, params): """Our model_fn for ResNet to be used with our Estimator.""" tf.summary.image('images', features, max_outputs=6) network = resnet_model.imagenet_resnet_v2(params['resnet_size'], _LABEL_CLASSES, params['data_format']) logits = network(inputs=features, is_training=(mode == tf.estimator.ModeKeys.TRAIN)) predictions = { 'classes': tf.argmax(logits, axis=1), 'probabilities': tf.nn.softmax(logits, name='softmax_tensor') } if mode == tf.estimator.ModeKeys.PREDICT: return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions) # Calculate loss, which includes softmax cross entropy and L2 regularization. cross_entropy = tf.losses.softmax_cross_entropy(logits=logits, onehot_labels=labels) # Create a tensor named cross_entropy for logging purposes. tf.identity(cross_entropy, name='cross_entropy') tf.summary.scalar('cross_entropy', cross_entropy) # Add weight decay to the loss. We exclude the batch norm variables because # doing so leads to a small improvement in accuracy. loss = cross_entropy + _WEIGHT_DECAY * tf.add_n([ tf.nn.l2_loss(v) for v in tf.trainable_variables() if 'batch_normalization' not in v.name ]) if mode == tf.estimator.ModeKeys.TRAIN: # Scale the learning rate linearly with the batch size. When the batch size # is 256, the learning rate should be 0.1. initial_learning_rate = 0.1 * params['batch_size'] / 256 batches_per_epoch = _NUM_IMAGES['train'] / params['batch_size'] global_step = tf.train.get_or_create_global_step() # Multiply the learning rate by 0.1 at 30, 60, 80, and 90 epochs. boundaries = [ int(batches_per_epoch * epoch) for epoch in [30, 60, 80, 90] ] values = [ initial_learning_rate * decay for decay in [1, 0.1, 0.01, 1e-3, 1e-4] ] learning_rate = tf.train.piecewise_constant( tf.cast(global_step, tf.int32), boundaries, values) # Create a tensor named learning_rate for logging purposes. tf.identity(learning_rate, name='learning_rate') tf.summary.scalar('learning_rate', learning_rate) optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=_MOMENTUM) # Batch norm requires update_ops to be added as a train_op dependency. update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_op = optimizer.minimize(loss, global_step) else: train_op = None accuracy = tf.metrics.accuracy(tf.argmax(labels, axis=1), predictions['classes']) metrics = {'accuracy': accuracy} # Create a tensor named train_accuracy for logging purposes. tf.identity(accuracy[1], name='train_accuracy') tf.summary.scalar('train_accuracy', accuracy[1]) return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions, loss=loss, train_op=train_op, eval_metric_ops=metrics)
def cifar10_model_fn(features, labels, mode, params): """Model function for CIFAR-10.""" tf.summary.image('images', features, max_outputs=6) if FLAGS.network == 'imagenet': network = resnet_model.imagenet_resnet_v2(params['resnet_size'], _NUM_CLASSES, params['data_format']) elif FLAGS.network == 'resnet_dropout': network = resnet_model.cifar10_resnet_dropout_generator( params['resnet_size'], _NUM_CLASSES, params['data_format'], keep_prob=FLAGS.keep_prob) elif FLAGS.network == 'resnet_bottleneck': network = resnet_model.cifar10_resnet_bottleneck_generator( params['resnet_size'], _NUM_CLASSES, params['data_format']) else: network = resnet_model.cifar10_resnet_v2_generator( params['resnet_size'], _NUM_CLASSES, params['data_format']) inputs = tf.reshape(features, [-1, _HEIGHT, _WIDTH, _DEPTH]) logits = network(inputs, mode == tf.estimator.ModeKeys.TRAIN) predictions = { 'classes': tf.argmax(logits, axis=1), 'probabilities': tf.nn.softmax(logits, name='softmax_tensor') } if mode == tf.estimator.ModeKeys.PREDICT: return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions) # Calculate loss, which includes softmax cross entropy and L2 regularization. cross_entropy = tf.losses.softmax_cross_entropy(logits=logits, onehot_labels=labels) # Create a tensor named cross_entropy for logging purposes. tf.identity(cross_entropy, name='cross_entropy') tf.summary.scalar('cross_entropy', cross_entropy) # Add weight decay to the loss. loss = cross_entropy + FLAGS.weight_decay * tf.add_n( [tf.nn.l2_loss(v) for v in tf.trainable_variables()]) if mode == tf.estimator.ModeKeys.TRAIN: # Scale the learning rate linearly with the batch size. When the batch size # is 128, the learning rate should be 0.1. initial_learning_rate = 0.1 * params['batch_size'] / 128 batches_per_epoch = _NUM_IMAGES['train'] / params['batch_size'] global_step = tf.train.get_or_create_global_step() # Multiply the learning rate by 0.1 at 60, 100, 200 and 170 epochs. boundaries = [ int(batches_per_epoch * epoch) for epoch in [60, 100, 150, 170] ] values = [ initial_learning_rate * decay for decay in [1, 0.1, 0.01, 0.001, 0.0001] ] learning_rate = tf.train.piecewise_constant( tf.cast(global_step, tf.int32), boundaries, values) # Create a tensor named learning_rate for logging purposes tf.identity(learning_rate, name='learning_rate') tf.summary.scalar('learning_rate', learning_rate) optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=_MOMENTUM) # Batch norm requires update ops to be added as a dependency to the train_op update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_op = optimizer.minimize(loss, global_step) else: train_op = None accuracy = tf.metrics.accuracy(tf.argmax(labels, axis=1), predictions['classes']) metrics = {'accuracy': accuracy} # Create a tensor named train_accuracy for logging purposes tf.identity(accuracy[1], name='train_accuracy') tf.summary.scalar('train_accuracy', accuracy[1]) return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions, loss=loss, train_op=train_op, eval_metric_ops=metrics)
loader_train = DataLoaderDisk(**opt_data_train) loader_val = DataLoaderDisk(**opt_data_val) #loader_train = DataLoaderH5(**opt_data_train) #loader_val = DataLoaderH5(**opt_data_val) # tf Graph input x = tf.placeholder(tf.float32, [None, fine_size, fine_size, c]) y = tf.placeholder(tf.int64, None) keep_dropout = tf.placeholder(tf.float32) train_phase = tf.placeholder(tf.bool) # Construct model # logits = alexnet(x, keep_dropout, train_phase) resnet_size = 18 num_classes = 100 resnet = resnet_model.imagenet_resnet_v2(resnet_size, num_classes) logits = resnet(x, True) # Define loss and optimizer loss = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=logits)) train_optimizer = tf.train.AdamOptimizer( learning_rate=learning_rate).minimize(loss) # Evaluate model accuracy1 = tf.reduce_mean(tf.cast(tf.nn.in_top_k(logits, y, 1), tf.float32)) accuracy5 = tf.reduce_mean(tf.cast(tf.nn.in_top_k(logits, y, 5), tf.float32)) # define initialization init = tf.global_variables_initializer()
def resnet_model_fn(features, labels, mode, params): """Our model_fn for ResNet to be used with our Estimator.""" tf.summary.image('images', features, max_outputs=6) network = resnet_model.imagenet_resnet_v2( params['resnet_size'], _LABEL_CLASSES, params['data_format']) logits = network( inputs=features, is_training=(mode == tf.estimator.ModeKeys.TRAIN)) predictions = { 'classes': tf.argmax(logits, axis=1), 'probabilities': tf.nn.softmax(logits, name='softmax_tensor') } if mode == tf.estimator.ModeKeys.PREDICT: return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions) # Calculate loss, which includes softmax cross entropy and L2 regularization. cross_entropy = tf.losses.softmax_cross_entropy( logits=logits, onehot_labels=labels) # Create a tensor named cross_entropy for logging purposes. tf.identity(cross_entropy, name='cross_entropy') tf.summary.scalar('cross_entropy', cross_entropy) # Add weight decay to the loss. We exclude the batch norm variables because # doing so leads to a small improvement in accuracy. loss = cross_entropy + _WEIGHT_DECAY * tf.add_n( [tf.nn.l2_loss(v) for v in tf.trainable_variables() if 'batch_normalization' not in v.name]) if mode == tf.estimator.ModeKeys.TRAIN: # Scale the learning rate linearly with the batch size. When the batch size # is 256, the learning rate should be 0.1. initial_learning_rate = 0.1 * params['batch_size'] / 256 batches_per_epoch = _NUM_IMAGES['train'] / params['batch_size'] global_step = tf.train.get_or_create_global_step() # Multiply the learning rate by 0.1 at 30, 60, 80, and 90 epochs. boundaries = [ int(batches_per_epoch * epoch) for epoch in [30, 60, 80, 90]] values = [ initial_learning_rate * decay for decay in [1, 0.1, 0.01, 1e-3, 1e-4]] learning_rate = tf.train.piecewise_constant( tf.cast(global_step, tf.int32), boundaries, values) # Create a tensor named learning_rate for logging purposes. tf.identity(learning_rate, name='learning_rate') tf.summary.scalar('learning_rate', learning_rate) optimizer = tf.train.MomentumOptimizer( learning_rate=learning_rate, momentum=_MOMENTUM) # Batch norm requires update_ops to be added as a train_op dependency. update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_op = optimizer.minimize(loss, global_step) else: train_op = None accuracy = tf.metrics.accuracy( tf.argmax(labels, axis=1), predictions['classes']) metrics = {'accuracy': accuracy} # Create a tensor named train_accuracy for logging purposes. tf.identity(accuracy[1], name='train_accuracy') tf.summary.scalar('train_accuracy', accuracy[1]) return tf.estimator.EstimatorSpec( mode=mode, predictions=predictions, loss=loss, train_op=train_op, eval_metric_ops=metrics)
def resnet_model_fn(features, labels, mode, params): """Our model_fn for ResNet to be used with our Estimator.""" tf.summary.image('images', features, max_outputs=6) network = resnet_model.imagenet_resnet_v2(params['resnet_size'], _LABEL_CLASSES, params['data_format']) logits = network(inputs=features, is_training=(mode == tf.estimator.ModeKeys.TRAIN)) print('<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>') print(logits.name) predictions = { 'classes': tf.argmax(logits, axis=1), 'probabilities': tf.nn.softmax(logits, name='softmax_tensor') } if mode == tf.estimator.ModeKeys.PREDICT: return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions) # Calculate loss, which includes softmax cross entropy and L2 regularization. cross_entropy = tf.losses.softmax_cross_entropy(logits=logits, onehot_labels=labels) #cross_entropy = -tf.reduce_sum(labels*tf.log(logits)) #cross_entropy = tf.losses.sparse_softmax_cross_entropy( #logits=logits, labels=labels) # Create a tensor named cross_entropy for logging purposes. tf.identity(cross_entropy, name='cross_entropy') tf.summary.scalar('cross_entropy', cross_entropy) # Add weight decay to the loss. We exclude the batch norm variables because # doing so leads to a small improvement in accuracy. loss = cross_entropy + _WEIGHT_DECAY * tf.add_n([ tf.nn.l2_loss(v) for v in tf.trainable_variables() if 'BatchNorm' not in v.name ]) if mode == tf.estimator.ModeKeys.TRAIN: # Scale the learning rate linearly with the batch size. When the batch size # is 256, the learning rate should be 0.1. #initial_learning_rate = 0.1 * params['batch_size'] / 256 initial_learning_rate = 0.05 batches_per_epoch = _NUM_IMAGES['train'] / params['batch_size'] global_step = tf.train.get_or_create_global_step() # Multiply the learning rate by 0.1 at 30, 60, 80, and 90 epochs. boundaries = [ int(batches_per_epoch * epoch) for epoch in [30, 60, 80, 90] ] #int(batches_per_epoch * epoch) for epoch in [20, 30, 40, 50]] values = [ initial_learning_rate * decay for decay in [1, 0.12, 0.06, 0.03, 0.03] ] learning_rate = tf.train.piecewise_constant( tf.cast(global_step, tf.int32), boundaries, values) g_values = [128., 128., 32., 8., 2.] g_scale = tf.train.piecewise_constant(tf.cast(global_step, tf.int32), boundaries, g_values) tf.identity(g_scale, name='g_scale') learning_rate = flr(learning_rate) # Create a tensor named learning_rate for logging purposes. tf.identity(learning_rate, name='learning_rate') tf.summary.scalar('learning_rate', learning_rate) optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=_MOMENTUM) # Batch norm requires update_ops to be added as a train_op dependency. update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) gradTrainBatch = optimizer.compute_gradients(loss) grad = [] var = [] for grad_and_vars in gradTrainBatch: grad.append(grad_and_vars[0]) var.append(grad_and_vars[1]) def QuantizeG(gradTrainBatch): grads = [] for grad_and_vars in gradTrainBatch: if grad_and_vars[1].name == 'conv2d/kernel:0' or grad_and_vars[ 1].name.find('dense') > -1: grads.append([grad_and_vars[0] * 1.0, grad_and_vars[1]]) elif grad_and_vars[1].name.find('BatchNorm') > -1: grads.append( [fgBN(grad_and_vars[0], 1.0), grad_and_vars[1]]) else: grads.append( [fg(grad_and_vars[0], 1.0, g_scale), grad_and_vars[1]]) return grads gradTrainBatch = QuantizeG(gradTrainBatch) Mom_Q = [] Mom_W = [] w_vars = tf.trainable_variables() for w_var in w_vars: if w_var.name == ( 'conv2d/kernel:0') or w_var.name.find('dense') > -1: Mom_W.append(tf.assign(w_var, w_var)) print(w_var.name) print('**************************') else: Mom_W.append(tf.assign(w_var, fBits(w_var, 24))) with tf.control_dependencies(update_ops): train_op = optimizer.apply_gradients(gradTrainBatch, global_step=global_step) opt_slot_name = optimizer.get_slot_names() train_vars = tf.trainable_variables() for train_var in train_vars: mom_var = optimizer.get_slot(train_var, opt_slot_name[0]) if train_var.name == ('conv2d/kernel:0' ) or train_var.name.find('dense') > -1: print(mom_var.name) else: Mom_Q.append(tf.assign(mom_var, fBits(mom_var, 13))) train_op = tf.group([train_op, Mom_Q, Mom_W]) else: train_op = None accuracy = tf.metrics.accuracy(tf.argmax(labels, axis=1), predictions['classes']) accuracy5 = tf.metrics.mean( tf.nn.in_top_k(logits, tf.argmax(labels, axis=1), k=5)) metrics = {'accuracy': accuracy, 'accuracy5': accuracy5} # Create a tensor named train_accuracy for logging purposes. tf.identity(accuracy[1], name='train_accuracy') tf.summary.scalar('train_accuracy', accuracy[1]) return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions, loss=loss, train_op=train_op, eval_metric_ops=metrics)
# 使用shuffle_batch可以随机打乱 next_batch挨着往下取 # shuffle_batch才能实现[img,label]的同步,也即特征和label的同步,不然可能输入的特征和label不匹配 # 比如只有这样使用,才能使img和label一一对应,每次提取一个image和对应的label # shuffle_batch返回的值就是RandomShuffleQueue.dequeue_many()的结果 # Shuffle_batch构建了一个RandomShuffleQueue,并不断地把单个的[img,label],送入队列中 image_batch, label_batch = tf.train.shuffle_batch( [image, label], batch_size=_batch_size, capacity=_batch_size * 4, allow_smaller_final_batch=True, min_after_dequeue=_batch_size * 2, num_threads=2) # 定义网络结构 network = resnet_model.imagenet_resnet_v2(_resnet_size, _num_classes, _data_format) with tf.Session() as sess: # inputs: a tensor of size [batch_size, height, width, channels] X = tf.reshape(x, [_batch_size, _height, _width, _depth]) # 数据输入网络得到输出值 logits = network(X, True) # 把标签转成one_hot形式 one_hot_labels = tf.one_hot(indices=tf.cast(y, tf.int32), depth=_num_classes) # 计算loss loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=one_hot_labels)) # 优化器
# Dataset Parameters model_dir = './build/resnet_adam_model' path_save = os.path.join(model_dir, 'model.ckpt-50801') images_dir = '../../data/images/test/' resnet_size = 50 data_format = 100 batch_size = 200 load_size = 256 fine_size = 224 c = 3 data_mean = np.asarray([0.45834960097, 0.44674252445, 0.41352266842]) classes = 100 write = False x = tf.placeholder(tf.float32, [None, fine_size, fine_size, c]) network = imagenet_resnet_v2(resnet_size, classes, None) logits = network(inputs=x, is_training=False) # read data info from lists def main(): idx = 0 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver = tf.train.import_meta_graph(path_save + ".meta") save_path = tf.train.latest_checkpoint(model_dir) saver.restore(sess, save_path)
def model(): with tf.name_scope('reshape'): # it's very important we make is_training a variable here, cuz we're using BN, which # behaives differently between training and inference is_training = tf.placeholder(tf.bool, name='is_training') trn_acc_labels_in = tf.placeholder(tf.float32, name='trn_acc_labels_in') trn_acc_logits_in = tf.placeholder(tf.float32, name='trn_acc_logits_in') tst_acc_labels_in = tf.placeholder(tf.float32, name='tst_acc_labels_in') tst_acc_logits_in = tf.placeholder(tf.float32, name='tst_acc_logits_in') x_in = tf.placeholder(tf.float32, name='x_in') y_in = tf.placeholder(tf.float32, name='y_in') learning_rate_in = tf.placeholder(tf.float32, name='learning_rate_in') x = tf.image.resize_images( tf.reshape(x_in, [-1, 75, 75, 2]), [224, 224], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) y = tf.reshape(y_in, [-1, 1]) with tf.name_scope('resnet'): resnet_v2 = resnet_model.imagenet_resnet_v2(18, 1, 'channels_last') y_ = resnet_v2(x, is_training) with tf.name_scope('sigmoid'): y_ = tf.sigmoid(y_) with tf.name_scope('cost'): cost = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=y_, labels=y)) with tf.name_scope('trn_cost'): trn_cost = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=trn_acc_logits_in, labels=trn_acc_labels_in)) tf.summary.scalar('trn_cost', trn_cost) with tf.name_scope('tst_cost'): tst_cost = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=tst_acc_logits_in, labels=tst_acc_labels_in)) tf.summary.scalar('tst_cost', tst_cost) with tf.name_scope('accuracy'): correct_prediction = tf.equal(y, tf.cast(y_ > 0.5, tf.float32)) correct_prediction = tf.cast(correct_prediction, tf.float32) accuracy = tf.reduce_mean(correct_prediction) with tf.name_scope('trn_acc'): trn_acc = tf.reduce_mean( tf.cast( tf.equal(trn_acc_labels_in, tf.cast(trn_acc_logits_in > 0.5, tf.float32)), tf.float32)) tf.summary.scalar("trn_acc", trn_acc) with tf.name_scope('tst_acc'): tst_acc = tf.reduce_mean( tf.cast( tf.equal(tst_acc_labels_in, tf.cast(tst_acc_logits_in > 0.5, tf.float32)), tf.float32)) tf.summary.scalar("tst_acc", tst_acc) with tf.name_scope('optimizer'): optimizer = tf.train.AdamOptimizer(learning_rate_in).minimize(cost) summ = tf.summary.merge_all() ret = { 'x_in': x_in, 'y_in': y_in, 'y_': y_, 'cost': cost, 'accuracy': accuracy, 'optimizer': optimizer, 'learning_rate_in': learning_rate_in, 'trn_acc_labels_in': trn_acc_labels_in, 'trn_acc_logits_in': trn_acc_logits_in, 'trn_acc': trn_acc, 'tst_acc_labels_in': tst_acc_labels_in, 'tst_acc_logits_in': tst_acc_logits_in, 'tst_acc': tst_acc, 'trn_cost': trn_cost, 'tst_cost': tst_cost, 'is_training': is_training, 'summ': summ } return ret
loader_train = DataLoaderDisk(**opt_data_train) loader_val = DataLoaderDisk(**opt_data_val) #loader_train = DataLoaderH5(**opt_data_train) #loader_val = DataLoaderH5(**opt_data_val) # tf Graph input x = tf.placeholder(tf.float32, [None, fine_size, fine_size, c]) y = tf.placeholder(tf.int64, None) keep_dropout = tf.placeholder(tf.float32) train_phase = tf.placeholder(tf.bool) # Construct model # logits = alexnet(x, keep_dropout, train_phase) params= {'resnet_size': 50, 'data_format': None, 'num_classes': 100} network = resnet_model.imagenet_resnet_v2(params['resnet_size'], params['num_classes'], params['data_format']) logits = network(inputs=x, is_training=train_phase) # Define loss and optimizer loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=logits)) train_optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss) # Evaluate model accuracy1 = tf.reduce_mean(tf.cast(tf.nn.in_top_k(logits, y, 1), tf.float32)) accuracy5 = tf.reduce_mean(tf.cast(tf.nn.in_top_k(logits, y, 5), tf.float32)) # define initialization init = tf.global_variables_initializer() # define saver saver = tf.train.Saver()
'fine_size': fine_size, 'data_mean': data_mean, 'randomize': False } # tf Graph input x = tf.placeholder(tf.float32, [None, fine_size, fine_size, c]) y = tf.placeholder(tf.int64, None) keep_dropout = tf.placeholder(tf.float32) train_phase = tf.placeholder(tf.bool) # Construct model # logits = alexnet(x, keep_dropout, train_phase) resnet_size = 18 num_classes = 100 resnet = resnet_model.imagenet_resnet_v2(resnet_size, num_classes) logits = resnet(x,True) # define initialization init = tf.global_variables_initializer() # define saver saver = tf.train.Saver() # Launch the graph with tf.Session() as sess: # Initialization if len(start_from)>1: saver.restore(sess, start_from) else: