def testModelHasExpectedNumberOfParameters(self): batch_size = 5 height, width = 299, 299 inputs = tf.random_uniform((batch_size, height, width, 3)) with slim.arg_scope(inception.inception_v3_arg_scope()): inception.inception_v3_base(inputs) total_params, _ = slim.model_analyzer.analyze_vars(slim.get_model_variables()) self.assertAlmostEqual(21802784, total_params)
def testModelHasExpectedNumberOfParameters(self): batch_size = 5 height, width = 299, 299 inputs = tf.random_uniform((batch_size, height, width, 3)) with slim.arg_scope(inception.inception_v3_arg_scope()): inception.inception_v3_base(inputs) total_params, _ = slim.model_analyzer.analyze_vars( slim.get_model_variables()) self.assertAlmostEqual(21802784, total_params)
def testBuildOnlyUptoFinalEndpoint(self): batch_size = 5 height, width = 299, 299 endpoints = [ "Conv2d_1a_3x3", "Conv2d_2a_3x3", "Conv2d_2b_3x3", "MaxPool_3a_3x3", "Conv2d_3b_1x1", "Conv2d_4a_3x3", "MaxPool_5a_3x3", "Mixed_5b", "Mixed_5c", "Mixed_5d", "Mixed_6a", "Mixed_6b", "Mixed_6c", "Mixed_6d", "Mixed_6e", "Mixed_7a", "Mixed_7b", "Mixed_7c", ] for index, endpoint in enumerate(endpoints): with tf.Graph().as_default(): inputs = tf.random_uniform((batch_size, height, width, 3)) out_tensor, end_points = inception.inception_v3_base(inputs, final_endpoint=endpoint) self.assertTrue(out_tensor.op.name.startswith("InceptionV3/" + endpoint)) self.assertItemsEqual(endpoints[: index + 1], end_points)
def testBuildAndCheckAllEndPointsUptoMixed7c(self): batch_size = 5 height, width = 299, 299 inputs = tf.random_uniform((batch_size, height, width, 3)) _, end_points = inception.inception_v3_base( inputs, final_endpoint='Mixed_7c') endpoints_shapes = {'Conv2d_1a_3x3': [batch_size, 149, 149, 32], 'Conv2d_2a_3x3': [batch_size, 147, 147, 32], 'Conv2d_2b_3x3': [batch_size, 147, 147, 64], 'MaxPool_3a_3x3': [batch_size, 73, 73, 64], 'Conv2d_3b_1x1': [batch_size, 73, 73, 80], 'Conv2d_4a_3x3': [batch_size, 71, 71, 192], 'MaxPool_5a_3x3': [batch_size, 35, 35, 192], 'Mixed_5b': [batch_size, 35, 35, 256], 'Mixed_5c': [batch_size, 35, 35, 288], 'Mixed_5d': [batch_size, 35, 35, 288], 'Mixed_6a': [batch_size, 17, 17, 768], 'Mixed_6b': [batch_size, 17, 17, 768], 'Mixed_6c': [batch_size, 17, 17, 768], 'Mixed_6d': [batch_size, 17, 17, 768], 'Mixed_6e': [batch_size, 17, 17, 768], 'Mixed_7a': [batch_size, 8, 8, 1280], 'Mixed_7b': [batch_size, 8, 8, 2048], 'Mixed_7c': [batch_size, 8, 8, 2048]} self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys()) for endpoint_name in endpoints_shapes: expected_shape = endpoints_shapes[endpoint_name] self.assertTrue(endpoint_name in end_points) self.assertListEqual(end_points[endpoint_name].get_shape().as_list(), expected_shape)
def inception_v3(inputs, num_classes=1000, is_training=True, dropout_keep_prob=0.8, spatial_squeeze=True, scope='InceptionV3'): with slim.arg_scope(inception.inception_v3_arg_scope( batch_norm_decay=BATCH_NORM_DECAY, batch_norm_epsilon=BATCH_NORM_EPSILON)): with slim.arg_scope( [slim.conv2d, slim.fully_connected, slim.batch_norm],trainable=True): with slim.arg_scope( [slim.batch_norm, slim.dropout], is_training=is_training): net, _ = inception.inception_v3_base( inputs, scope=scope) with tf.variable_scope('Logits'): kernel_size = _reduced_kernel_size_for_small_input(net, [8, 8]) net = tf.contrib.layers.avg_pool2d( net, kernel_size, padding='VALID') if is_training: net = tf.contrib.layers.dropout( net, keep_prob=dropout_keep_prob) logits = tf.contrib.layers.conv2d( net, num_classes, [1, 1], activation_fn=None, normalizer_fn=None) if spatial_squeeze: logits = tf.squeeze(logits, [1, 2], name='SpatialSqueeze') return logits
def __init__(self, sess, dim=299): self.batch_shape = [16, dim, dim, 3] self._num_classes = 1001 self._scope = 'InceptionV3' self._weights_file = './Weights/inception_v3.ckpt' output_layer = 'Conv2d_1a_3x3' # # network inputs # self.x_tf = tf.placeholder(tf.float32, shape=self.batch_shape) # # network outputs # with slim.arg_scope(inception.inception_v3_arg_scope()): with arg_scope([layers_lib.batch_norm, layers_lib.dropout], is_training=False): # we truncate before these layers, so this is not really necessary... net, endpoints = inception.inception_v3_base(self.x_tf, final_endpoint=output_layer, scope=self._scope) self.output = endpoints[output_layer] # # load weights # saver = tf.train.Saver(slim.get_model_variables(scope=self._scope)) saver.restore(sess, self._weights_file)
def conv_tower_fn(self, images, is_training=True, reuse=None): """Computes convolutional features using the InceptionV3 model. Args: images: A tensor of shape [batch_size, height, width, channels]. is_training: whether is training or not. reuse: whether or not the network and its variables should be reused. To be able to reuse 'scope' must be given. Returns: A tensor of shape [batch_size, OH, OW, N], where OWxOH is resolution of output feature map and N is number of output features (depends on the network architecture). """ mparams = self._mparams["conv_tower_fn"] logging.debug("Using final_endpoint=%s", mparams.final_endpoint) with tf.variable_scope("conv_tower_fn/INCE"): if reuse: tf.get_variable_scope().reuse_variables() with slim.arg_scope(inception.inception_v3_arg_scope()): with slim.arg_scope([slim.batch_norm, slim.dropout], is_training=is_training): net, _ = inception.inception_v3_base( images, final_endpoint=mparams.final_endpoint) return net
def create_stream(self, data_input, dropout, trainable): self.batch_norm_params['trainable'] = trainable with slim.arg_scope([slim.conv2d], padding='SAME', activation_fn=self.activation_function, weights_initializer=self.weight_init, weights_regularizer=self.weight_decay, #normalizer_fn=self.normalizer_function, normalizer_params=self.batch_norm_params, trainable=trainable): with slim.arg_scope([slim.batch_norm], **self.batch_norm_params): last_output, layers = inception.inception_v3_base(data_input, scope='Inception_V3', final_endpoint=self.endpoint) # Global average pooling last_output = tf.reduce_mean(last_output, [1, 2]) last_output = tf.reshape(last_output, [-1, self.n_fc]) last_output = slim.fully_connected(last_output, self.n_fc, scope='fc0', trainable=trainable) if dropout is not None: last_output = slim.dropout(last_output, keep_prob=dropout, scope='dropout_0') # Pose Regression last_output = slim.fully_connected(last_output, 7, normalizer_fn=None, scope='fc1', activation_fn=None, weights_initializer=self.weight_init, weights_regularizer=self.weight_decay, trainable=trainable) layers['last_output'] = last_output self.layers = layers return self.slice_output(last_output), layers
def conv_tower_fn(self, images, is_training=True, reuse=None): """Computes convolutional features using the InceptionV3 model. Args: images: A tensor of shape [batch_size, height, width, channels]. is_training: whether is training or not. reuse: whether or not the network and its variables should be reused. To be able to reuse 'scope' must be given. Returns: A tensor of shape [batch_size, OH, OW, N], where OWxOH is resolution of output feature map and N is number of output features (depends on the network architecture). """ mparams = self._mparams['conv_tower_fn'] logging.debug('Using final_endpoint=%s', mparams.final_endpoint) with tf.variable_scope('conv_tower_fn/INCE'): if reuse: tf.get_variable_scope().reuse_variables() with slim.arg_scope(inception.inception_v3_arg_scope()): with slim.arg_scope([slim.batch_norm, slim.dropout], is_training=is_training): net, _ = inception.inception_v3_base( images, final_endpoint=mparams.final_endpoint) return net
def build_model(model_name, inputs, num_classes, is_training, dropout_keep_prob): use_fcn = False if model_name.find('fcn') >= 0: use_fcn = True model_base_name = model_name[0:-4] else: model_base_name = model_name if model_base_name == 'vgg16': net = vgg16_base(inputs) elif model_base_name == 'inception_v1': with slim.arg_scope(inception.inception_v1_arg_scope()): net, _ = inception.inception_v1_base(inputs) elif model_base_name == 'inception_v2': with slim.arg_scope(inception.inception_v2_arg_scope()): net, _ = inception.inception_v2_base(inputs) elif model_base_name == 'inception_v3': with slim.arg_scope(inception.inception_v3_arg_scope()): net, _ = inception.inception_v3_base(inputs) else: raise Exception('model {} is not existed'.format(model_name)) with tf.variable_scope('not_pretrained'): if use_fcn: net = fully_convolutional_networks(net, num_classes, is_training, dropout_keep_prob) else: net = fully_connected_networks(net, num_classes, is_training, dropout_keep_prob) return net
def testBuildBaseNetwork(self): batch_size = 5 height, width = 299, 299 inputs = tf.random_uniform((batch_size, height, width, 3)) final_endpoint, end_points = inception.inception_v3_base(inputs) self.assertTrue(final_endpoint.op.name.startswith("InceptionV3/Mixed_7c")) self.assertListEqual(final_endpoint.get_shape().as_list(), [batch_size, 8, 8, 2048]) expected_endpoints = [ "Conv2d_1a_3x3", "Conv2d_2a_3x3", "Conv2d_2b_3x3", "MaxPool_3a_3x3", "Conv2d_3b_1x1", "Conv2d_4a_3x3", "MaxPool_5a_3x3", "Mixed_5b", "Mixed_5c", "Mixed_5d", "Mixed_6a", "Mixed_6b", "Mixed_6c", "Mixed_6d", "Mixed_6e", "Mixed_7a", "Mixed_7b", "Mixed_7c", ] self.assertItemsEqual(end_points.keys(), expected_endpoints)
def conv_tower_fn(self,images, is_training = True, reuse = None): mparams = self._mparams['conv_tower_fn'] logging.debug('Using final_endpoint=%s',mparams.final_endpoint) if reuse: tf.get_variable_scope().reuse_variables() with slim.arg_scope(inception.inception_v3_arg_scope()): #用slim.arg_scope设置默认参数,但是,设置参数需要用于被@add_arg_scope修饰过的参数,见源码 with slim.arg_scope([slim.batch_norm,slim.dropout],is_training=is_training): net,_ = inception.inception_v3_base(images,final_endpoint=mparams.final_endpoint) return net
def build(self, inputs, input_pixel_size, is_training, scope='img_inception'): """Inception for BEV feature extraction Args: inputs: a tensor of size [batch_size, height, width, channels]. input_pixel_size: size of the input (H x W) is_training: True for training, False fo validation/testing. scope: Optional scope for the variables. Returns: The net, a rank-4 tensor of size [batch, height_out, width_out, channels_out] and end_points dict. """ inception_config = self.config with tf.variable_scope(scope, 'img_inception', [inputs]) as scope: with slim.arg_scope([slim.batch_norm, slim.dropout], is_training=is_training): if inception_config.inception_v == 'inception_v1': with slim.arg_scope(inception.inception_v1_arg_scope()): net, end_points = inception.inception_v1_base( inputs, scope=scope) elif inception_config.inception_v == 'inception_v2': with slim.arg_scope(inception.inception_v2_arg_scope()): net, end_points = inception.inception_v2_base( inputs, scope=scope) elif inception_config.inception_v == 'inception_v3': with slim.arg_scope(inception.inception_v3_arg_scope()): net, end_points = inception.inception_v3_base( inputs, scope=scope) else: raise ValueError('Invalid Inception version {},'.format( inception_config.inception_v)) with tf.variable_scope('upsampling'): # This feature extractor downsamples the input by a factor # of 32 downsampling_factor = 32 downsampled_shape = input_pixel_size / downsampling_factor upsampled_shape = downsampled_shape * \ inception_config.upsampling_multiplier feature_maps_out = tf.image.resize_bilinear( net, upsampled_shape) return feature_maps_out, end_points
def _build_perception_network(visual_input): with tf.variable_scope('perception'): with arg_scope(inception.inception_v3_arg_scope()): inception_features, _ = inception.inception_v3_base( visual_input) net = slim.layers.avg_pool2d(inception_features, [8, 8]) if config["freeze_cnn"]: net = tf.stop_gradient(net) net = slim.flatten(net) net = slim.layers.fully_connected(net, config["hidden_size"], activation_fn=tf.nn.relu) return net
def testBuildBaseNetwork(self): batch_size = 5 height, width = 299, 299 inputs = tf.random_uniform((batch_size, height, width, 3)) final_endpoint, end_points = inception.inception_v3_base(inputs) self.assertTrue(final_endpoint.op.name.startswith( 'InceptionV3/Mixed_7c')) self.assertListEqual(final_endpoint.get_shape().as_list(), [batch_size, 8, 8, 2048]) expected_endpoints = ['Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3', 'MaxPool_3a_3x3', 'Conv2d_3b_1x1', 'Conv2d_4a_3x3', 'MaxPool_5a_3x3', 'Mixed_5b', 'Mixed_5c', 'Mixed_5d', 'Mixed_6a', 'Mixed_6b', 'Mixed_6c', 'Mixed_6d', 'Mixed_6e', 'Mixed_7a', 'Mixed_7b', 'Mixed_7c'] self.assertItemsEqual(end_points.keys(), expected_endpoints)
def create_tf_graph(self, img): self.graph = tf.Graph() with self.graph.as_default(): self.image = tf.placeholder(tf.float32, shape=img.shape) processed_image = preprocess_image(self.image, image_size, image_size) processed_images = tf.expand_dims(processed_image, 0) # Create the model, use the default arg scope to configure the batch norm parameters. with slim.arg_scope(inception.inception_v3_arg_scope()): self.tensor_out, endpoints = inception.inception_v3_base(processed_images, final_endpoint='Conv2d_4a_3x3') self.init_fn = slim.assign_from_checkpoint_fn(os.path.abspath('checkpoint/inception_v3.ckpt'), slim.get_model_variables('InceptionV3')) self.session = tf.Session(graph=self.graph) self.init_fn(self.session)
def _one_frame_forward(i, lstm_state, Q_vals, chose_to_release): """Runs one time-step forward from the input frame to the output Qs including epsilon-greedy action choice.""" with tf.variable_scope('one_frame_forward', reuse=tf.AUTO_REUSE): # Get current frame curr_input_frame = input_frames[ i:i + 1] # The :+1 keeps the dimension # Pass through inception_v3 # Note: use_fused_batchnorm = False is to work around a bug that breaks # backprop through fused_batchnorm. with arg_scope( inception.inception_v3_arg_scope( use_fused_batchnorm=False)): inception_features, _ = inception.inception_v3_base( curr_input_frame) # Flatten and fully-connect to lstm inputs inception_features = slim.flatten(inception_features, scope='inception_flattened') if not model_config['tune_vision_model']: inception_features = tf.stop_gradient(inception_features) lstm_inputs = slim.fully_connected( inception_features, model_config['LSTM_hidden_size'], activation_fn=tf.nn.relu, scope='lstm_inputs') # LSTM! (lstm_outputs, lstm_state) = lstm_cell(lstm_inputs, lstm_state) # Fully connect (linear) to Q estimates Q_vals = slim.fully_connected(lstm_outputs, num_actions, activation_fn=None, scope='Q_vals') # is the greedy action to release? greedy_release = tf.less(Q_vals[0, 0], Q_vals[0, 1]) # is this an epsilon-exploring trial? explore = tf.less(explore_vals[i], epsilon_ph) # make the choice thereby chose_to_release = tf.logical_xor(greedy_release, explore) return (i, lstm_state, Q_vals, tf.squeeze(chose_to_release))
def testBuildOnlyUptoFinalEndpoint(self): batch_size = 5 height, width = 299, 299 endpoints = ['Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3', 'MaxPool_3a_3x3', 'Conv2d_3b_1x1', 'Conv2d_4a_3x3', 'MaxPool_5a_3x3', 'Mixed_5b', 'Mixed_5c', 'Mixed_5d', 'Mixed_6a', 'Mixed_6b', 'Mixed_6c', 'Mixed_6d', 'Mixed_6e', 'Mixed_7a', 'Mixed_7b', 'Mixed_7c'] for index, endpoint in enumerate(endpoints): with tf.Graph().as_default(): inputs = tf.random_uniform((batch_size, height, width, 3)) out_tensor, end_points = inception.inception_v3_base( inputs, final_endpoint=endpoint) self.assertTrue(out_tensor.op.name.startswith( 'InceptionV3/' + endpoint)) self.assertItemsEqual(endpoints[:index+1], end_points)
def create_tf_graph(self, img): self.graph = tf.Graph() with self.graph.as_default(): print("ok") self.image = tf.placeholder(tf.float32, shape=img.shape) processed_image = preprocess_image(self.image, image_size, image_size) processed_images = tf.expand_dims(processed_image, 0) # Create the model, use the default arg scope to configure the batch norm parameters. with slim.arg_scope(inception.inception_v3_arg_scope()): self.tensor_out, endpoints = inception.inception_v3_base(processed_images, final_endpoint='Conv2d_4a_3x3') #self.init_fn = slim.assign_from_checkpoint_fn(os.path.abspath('checkpoint/inception_v3.ckpt'), # slim.get_model_variables('InceptionV3')) self.init_fn = slim.assign_from_checkpoint_fn('inception_v3.ckpt', slim.get_model_variables('InceptionV3')) gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333) config = tf.ConfigProto(gpu_options=gpu_options) config.gpu_options.allow_growth=True self.session = tf.Session(graph=self.graph, config=config) self.init_fn(self.session)
def build_model(self): with tf.name_scope('inputs'): self.X = tf.placeholder(tf.float32, shape=[ None, self.height_width, self.height_width, self.channels ], name="X") self.y_true = tf.placeholder(tf.int64, shape=[None, self.num_classes], name='y_true') y_true_cls = tf.argmax(self.y_true, dimension=1) self.is_traing = tf.placeholder(tf.bool, name='is_traing') # Normalizing the images self.X = tf.map_fn(lambda img: tf.image.per_image_standardization(img), self.X) with tf.name_scope('data_augmentation'): self.X = tf.map_fn( lambda img: tf.image.random_brightness(img, max_delta=20 / 255 ), self.X) self.X = tf.map_fn( lambda img: tf.image.random_contrast(img, lower=0.5, upper=2), self.X) # inception layer if self.layer_name == 'PreLogits': # Running on the entire network, remove only the last layer with slim.arg_scope(inception.inception_v3_arg_scope()): _, end_points = inception.inception_v3(self.X, num_classes=1001, is_training=is_traing) end_transfer_net = tf.squeeze(end_points[self.layer_name], axis=[1, 2]) else: with slim.arg_scope(inception.inception_v3_arg_scope()): end_transfer_net, _ = inception.inception_v3_base( self.X, final_endpoint=self.layer_name, min_depth=16, depth_multiplier=1.0, scope=None) tf.summary.histogram(self.layer_name, end_transfer_net) self.inception_saver = tf.train.Saver() if self.layer_name != 'PreLogits': # extra CNN for Identifying features focused on mammograms self.conv_net, self.w1 = self.conv2d(end_transfer_net, self.nfs, k_h=self.fs, k_w=self.fs, d_h=1, d_w=1, stddev=0.02, name='conv', padding='VALID') tf.summary.histogram('conv', self.conv_net) tf.summary.histogram('Wight_conv', self.w1) x_maxpool = tf.nn.max_pool(value=self.conv_net, ksize=[ 1, self.conv_net.get_shape()[1], self.conv_net.get_shape()[1], 1 ], strides=[1, 2, 2, 1], padding='VALID') end_transfer_net = tf.nn.relu(x_maxpool) self.flatt = flatten(end_transfer_net, scope='flatten') tf.summary.histogram('flatten', self.flatt) else: self.flatt = end_transfer_net tf.summary.histogram('flatten', self.flatt) self.x_hidden = fully_connected(self.flatt, self.n_hidden, scope='hidden') tf.summary.histogram('hidden', self.x_hidden) self.logits = fully_connected(self.x_hidden, self.num_classes, activation_fn=None, scope='output') tf.summary.histogram('logits', self.logits) with tf.name_scope('predicted'): self.y_pred = tf.nn.softmax(self.logits) self.y_pred_cls = tf.argmax(self.y_pred, dimension=1) with tf.name_scope('loss'): cross_entropy = tf.nn.softmax_cross_entropy_with_logits( logits=self.logits, labels=self.y_true, name='xentropy') self.loss = tf.reduce_mean(cross_entropy, name='xentropy_mean') tf.summary.scalar('loss', self.loss) with tf.name_scope('performance'): correct_prediction = tf.equal(self.y_pred_cls, y_true_cls) self.accuracy = tf.reduce_mean( tf.cast(correct_prediction, tf.float32)) self.merged = tf.summary.merge_all( ) # merge the data for the tensorboard self.saver = tf.train.Saver(max_to_keep=1)
image_size = inception.inception_v3.default_image_size names = imagenet.create_readable_names_for_imagenet_labels() with tf.Graph().as_default(): filename_queue = tf.train.string_input_producer( ['/home/tiago/Desktop/img.jpeg', '/home/tiago/Desktop/img2.jpeg', '/home/tiago/Desktop/img3.jpeg'], shuffle=False) reader = tf.WholeFileReader() key, value = reader.read(filename_queue) image = tf.image.decode_jpeg(value, channels=3) processed_image = inception_preprocessing.preprocess_image(image, image_size, image_size, is_training=False) processed_images = tf.expand_dims(processed_image, 0) # Create the model, use the default arg scope to configure the batch norm parameters. with slim.arg_scope(inception.inception_v3_arg_scope()): tensor_out, endpoints = inception.inception_v3_base(processed_images, final_endpoint='Conv2d_2b_3x3') #logits, _ = inception.inception_v3(processed_images, num_classes=1001, is_training=False) #probabilities = tf.nn.softmax(logits) init_fn = slim.assign_from_checkpoint_fn('/home/tiago/PycharmProjects/DeepTrack/inception_v3.ckpt', slim.get_model_variables('InceptionV3')) with tf.Session() as sess: init_fn(sess) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) for i in range(3): smt = tensor_out.eval() print(smt.shape) for j in range(3): plt.imshow(smt[0,:,:,10+j])