Ejemplo n.º 1
0
    def __init__(self, name):
        super(Classifier, self).__init__(name)
        file_handler = RotatingFileHandler(FLAGS.log,
                                           maxBytes=1024 * 1024 * 100,
                                           backupCount=20)
        file_handler.setLevel(logging.INFO)
        formatter = logging.Formatter(
            "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
        file_handler.setFormatter(formatter)
        self.logger.addHandler(file_handler)
        self.names = create_readable_names_for_imagenet_labels()
        self.image_size = default_image_size

        self.image_str_placeholder = tf.placeholder(tf.string)
        image = tf.image.decode_jpeg(self.image_str_placeholder, channels=3)
        processed_image = preprocess_image(image, self.image_size,
                                           self.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_v4_arg_scope()):
            logits, _ = inception_v4(processed_images,
                                     num_classes=1001,
                                     is_training=False)
        self.probabilities = tf.nn.softmax(logits)

        dest_directory = FLAGS.model_dir
        init_fn = slim.assign_from_checkpoint_fn(
            os.path.join(dest_directory, 'inception_v4.ckpt'),
            slim.get_model_variables('InceptionV4'))

        self.sess = tf.Session()
        init_fn(self.sess)
Ejemplo n.º 2
0
    def buildNN(self,x,reuse=False,isTraining=True):
        h = x

        # Slim
        with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
            _,end_points = inception_v4.inception_v4(h,is_training=isTraining,reuse=reuse,create_aux_logits=False)
        h = end_points["Mixed_5b"]
        if not reuse:
            self.variables_to_restore_bbNet = slim.get_variables_to_restore()

        with tf.variable_scope("NN") as scope:
            if reuse: scope.reuse_variables()

            # avgpool
            n_b, n_h, n_w, n_f = self.getShape(h)
            h = tf.nn.avg_pool(h,ksize=[1,n_h,n_w,1],strides=[1,1,1,1],padding="VALID")

            # fc1
            n_b, n_h, n_w, n_f = self.getShape(h)
            assert n_h==n_w==1, "invalid shape after avg pool:(%d,%d)"%(n_h,n_w)
            h = tf.reshape(h,[n_b,n_f])
            self.fc1_w, self.fc1_b = self._fc_variable([n_f,self.zdim],name="fc1")
            h = tf.matmul(h, self.fc1_w) + self.fc1_b

            # L2 normalization
            h = h / tf.norm(h,axis=1,keep_dims=True)

        if not reuse:
            tf.summary.histogram("fc1_w"   ,self.fc1_w)
            tf.summary.histogram("fc1_b"   ,self.fc1_b)

        return h
Ejemplo n.º 3
0
    def __init__(self, name):
        super(Classifier, self).__init__(name)
        file_handler = RotatingFileHandler(FLAGS.log, maxBytes=1024 * 1024 * 100, backupCount=20)
        file_handler.setLevel(logging.INFO)
        formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
        file_handler.setFormatter(formatter)
        self.logger.addHandler(file_handler)
        self.names = create_readable_names_for_imagenet_labels()
        self.image_size = default_image_size

        self.image_str_placeholder = tf.placeholder(tf.string)
        image = tf.image.decode_jpeg(self.image_str_placeholder, channels=3)
        processed_image = preprocess_image(image, self.image_size, self.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_v4_arg_scope()):
            logits, _ = inception_v4(processed_images, num_classes=1001, is_training=False)
        self.probabilities = tf.nn.softmax(logits)

        dest_directory = FLAGS.model_dir
        init_fn = slim.assign_from_checkpoint_fn(
            os.path.join(dest_directory, 'inception_v4.ckpt'),
            slim.get_model_variables('InceptionV4'))

        self.sess = tf.Session()
        init_fn(self.sess)
def main(_):
    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    num_classes = 1001

    tf.logging.set_verbosity(tf.logging.INFO)

    with tf.Graph().as_default():
        # Prepare graph
        x_input = tf.placeholder(tf.float32, shape=batch_shape)

        with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
            _, end_points = inception_v4.inception_v4(x_input,
                                                      num_classes=num_classes,
                                                      is_training=False)

        predicted_labels = tf.argmax(end_points['Predictions'], 1)

        # Run computation
        saver = tf.train.Saver(slim.get_model_variables())
        session_creator = tf.train.ChiefSessionCreator(
            scaffold=tf.train.Scaffold(saver=saver),
            checkpoint_filename_with_path=FLAGS.checkpoint_path,
            master=FLAGS.master)

        with tf.train.MonitoredSession(
                session_creator=session_creator) as sess:
            with tf.gfile.Open(FLAGS.output_file, 'w') as out_file:
                for filenames, images in load_images(FLAGS.input_dir,
                                                     batch_shape):
                    labels = sess.run(predicted_labels,
                                      feed_dict={x_input: images})
                    for filename, label in zip(filenames, labels):
                        out_file.write('{0},{1}\n'.format(filename, label))
Ejemplo n.º 5
0
def test():
    model_dir = '/Users/alexwang/data/'
    checkpoint_path = os.path.join(model_dir, 'inception_v4.ckpt')
    img_path = '../data/laska.png'
    img = cv2.imread(img_path)
    print('shape of img:', img.shape)

    # preprocess image
    batch_size = 2
    image_size = inception_v4.inception_v4.default_image_size
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.
    preprocessed_image_one = inception_preprocessing.preprocess_for_eval(
        tf.convert_to_tensor(img_rgb, tf.float32),
        image_size,
        image_size,
        central_fraction=1)

    zeros_mat = np.zeros(shape=(image_size, image_size, 3), dtype=np.float32)
    preprocessed_image_two = inception_preprocessing.preprocess_for_eval(
        tf.convert_to_tensor(zeros_mat, tf.float32),
        image_size,
        image_size,
        central_fraction=1)
    inputs = tf.concat([
        tf.expand_dims(preprocessed_image_one, 0),
        tf.expand_dims(preprocessed_image_two, 0)
    ],
                       axis=0)

    # construct net
    # print_tensors_in_checkpoint_file(checkpoint_path, None, False)
    arg_scope = inception_v4.inception_v4_arg_scope()
    with slim.arg_scope(arg_scope):
        logits, end_points = inception_v4.inception_v4(inputs,
                                                       is_training=False,
                                                       num_classes=1001)
    for tensor in tf.all_variables():
        print(tensor.name, tensor.shape)

    probabilities = tf.nn.softmax(logits)

    # restore net
    saver = tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess, checkpoint_path)

        # predict
        inputs_value, probabilitie_values, end_points_values = sess.run(
            [inputs, probabilities, end_points])
        print('shape of inputs:', inputs_value.shape)
        print('shape of predict', probabilitie_values.shape)
        # print('end_point:', end_points_values)
        for key in end_points_values.keys():
            print(key, end_points_values[key].shape)

        for predict in probabilitie_values:
            sorted_predict = np.argsort(predict)
            top_5 = sorted_predict[::-1][0:5]
            top_5_labels = [(label_names[i], predict[i]) for i in top_5]
            print(top_5_labels)
Ejemplo n.º 6
0
 def arch_inception_v4_rnn(self, X, num_classes, dropout_keep_prob=0.8, is_train=False):
     rnn_size = 256  
     num_layers = 2  
     arg_scope = inception_v4_arg_scope()
     with slim.arg_scope(arg_scope):
         net_vis, end_points = inception_v4(X, is_training=is_train)
     with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d], stride=1, padding='SAME'):
         with tf.variable_scope('Logits_out'):
             # 8 x 8 x 1536
             orig_shape = net_vis.get_shape().as_list()
             net = tf.reshape(net_vis, [-1, orig_shape[1] * orig_shape[2], orig_shape[3]])
             
             def gru_cell():
                 return tf.contrib.rnn.GRUCell(rnn_size)
             def lstm_cell():  
                 return tf.contrib.rnn.LSTMCell(rnn_size)  
             def attn_cell():  
                 return tf.contrib.rnn.DropoutWrapper(lstm_cell(), output_keep_prob=dropout_keep_prob)  
             stack = tf.contrib.rnn.MultiRNNCell([lstm_cell() for _ in range(0, num_layers)], state_is_tuple=True)  
             net, _ = tf.nn.dynamic_rnn(stack, net, dtype=tf.float32)  
             net = tf.transpose(net, (1, 0, 2))
             # 1536
             net = slim.fully_connected(net[-1], 256, activation_fn=tf.nn.relu, scope='Logits_out0')
             net = slim.fully_connected(net, num_classes, activation_fn=None,scope='Logits_out1')
     return net, net_vis
Ejemplo n.º 7
0
 def network_fn(images, **kwargs):
     arg_scope = inception_v4_arg_scope(weight_decay=weight_decay)
     with slim.arg_scope(arg_scope):
         return func(images,
                     num_classes=num_classes,
                     is_training=is_training,
                     **kwargs)
Ejemplo n.º 8
0
 def arch_inception_v4(self,
                       X,
                       num_classes,
                       dropout_keep_prob=0.8,
                       is_train=False):
     arg_scope = inception_v4_arg_scope()
     with slim.arg_scope(arg_scope):
         net_vis, end_points = inception_v4(X, is_training=is_train)
     with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d],
                         stride=1,
                         padding='SAME'):
         with tf.variable_scope('Logits_out'):
             # 8 x 8 x 1536
             net = slim.avg_pool2d(net_vis,
                                   net_vis.get_shape()[1:3],
                                   padding='VALID',
                                   scope='AvgPool_1a_out')
             # 1 x 1 x 1536
             net = slim.dropout(net,
                                dropout_keep_prob,
                                scope='Dropout_1b_out')
             net = slim.flatten(net, scope='PreLogitsFlatten_out')
             # 1536
             net = slim.fully_connected(net,
                                        256,
                                        activation_fn=tf.nn.relu,
                                        scope='Logits_out0')
             net = slim.fully_connected(net,
                                        num_classes,
                                        activation_fn=None,
                                        scope='Logits_out1')
     return net, net_vis
def build_frontend(inputs,
                   frontend,
                   is_training=True,
                   pretrained_dir="models"):
    if frontend == 'ResNet50':
        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            logits, end_points = resnet_v2.resnet_v2_50(
                inputs, is_training=is_training, scope='resnet_v2_50')
            frontend_scope = 'resnet_v2_50'
            init_fn = slim.assign_from_checkpoint_fn(
                model_path=os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'),
                var_list=slim.get_model_variables('resnet_v2_50'),
                ignore_missing_vars=True)
    elif frontend == 'ResNet101':
        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            logits, end_points = resnet_v2.resnet_v2_101(
                inputs, is_training=is_training, scope='resnet_v2_101')
            frontend_scope = 'resnet_v2_101'
            init_fn = slim.assign_from_checkpoint_fn(
                model_path=os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'),
                var_list=slim.get_model_variables('resnet_v2_101'),
                ignore_missing_vars=True)
    elif frontend == 'ResNet152':
        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            logits, end_points = resnet_v2.resnet_v2_152(
                inputs, is_training=is_training, scope='resnet_v2_152')
            frontend_scope = 'resnet_v2_152'
            init_fn = slim.assign_from_checkpoint_fn(
                model_path=os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'),
                var_list=slim.get_model_variables('resnet_v2_152'),
                ignore_missing_vars=True)
    elif frontend == 'MobileNetV2':
        with slim.arg_scope(mobilenet_v2.training_scope()):
            logits, end_points = mobilenet_v2.mobilenet(
                inputs,
                is_training=is_training,
                scope='mobilenet_v2',
                base_only=True)
            frontend_scope = 'mobilenet_v2'
            init_fn = slim.assign_from_checkpoint_fn(
                model_path=os.path.join(pretrained_dir,
                                        'mobilenet_v2_1.4_224.ckpt'),
                var_list=slim.get_model_variables('mobilenet_v2'),
                ignore_missing_vars=True)
    elif frontend == 'InceptionV4':
        with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
            logits, end_points = inception_v4.inception_v4(
                inputs, is_training=is_training, scope='inception_v4')
            frontend_scope = 'inception_v4'
            init_fn = slim.assign_from_checkpoint_fn(
                model_path=os.path.join(pretrained_dir, 'inception_v4.ckpt'),
                var_list=slim.get_model_variables('inception_v4'),
                ignore_missing_vars=True)
    else:
        raise ValueError(
            "Unsupported fronetnd model '%s'. This function only supports ResNet50, ResNet101, ResNet152, and MobileNetV2"
            % (frontend))

    return logits, end_points, frontend_scope, init_fn
Ejemplo n.º 10
0
def predict(image, version='V3'):
    tf.reset_default_graph()
    
    # Process the image 
    raw_image, processed_image = process_image(image)
    print(raw_image.shape)
    class_names = imagenet.create_readable_names_for_imagenet_labels()
    
    # Create a placeholder for the images
    X = tf.placeholder(tf.float32, [None, 299, 299, 3], name="X")
    
    '''
    inception_v3 function returns logits and end_points dictionary
    logits are output of the network before applying softmax activation
    '''
    
    if version.upper() == 'V3':
        print("V3!!")
        model_ckpt_path = INCEPTION_V3_CKPT_PATH
        with tf.contrib.slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            # Set the number of classes and is_training parameter  
            logits, end_points = inception_v3.inception_v3(X, num_classes=1001, is_training=False)
            
    elif version.upper() == 'V4':
        model_ckpt_path = INCEPTION_V4_CKPT_PATH
        with tf.contrib.slim.arg_scope(inception_v4.inception_v4_arg_scope()):
            # Set the number of classes and is_training parameter
            # Logits 
            logits, end_points = inception_v4.inception_v4(X, num_classes=1001, is_training=False)
            
    
    predictions = end_points.get('Predictions', 'No key named predictions')
    saver = tf.train.Saver()
    
    with tf.Session() as sess:
        print("model_ckpt_path", model_ckpt_path)
        saver.restore(sess, model_ckpt_path)
        prediction_values = predictions.eval({X: processed_image})
        
    try:
        # Add an index to predictions and then sort by probability
        prediction_values = [(i, prediction) for i, prediction in enumerate(prediction_values[0,:])]
        prediction_values = sorted(prediction_values, key=lambda x: x[1], reverse=True)
        
        # Plot the image
        #plot_color_image(raw_image)
        #plt.show()
        print("Using Inception_{} CNN\nPrediction: Probability\n".format(version))
        # Display the image and predictions 
        for i in range(10):
            predicted_class = class_names[prediction_values[i][0]]
            probability = prediction_values[i][1]
            print("{}: {:.2f}%".format(predicted_class, probability*100))
    
    # If the predictions do not come out right
    except:
        print(predictions)
Ejemplo n.º 11
0
 def __call__(self, x_input):
     """Constructs model and return probabilities for given input."""
     reuse = True if self.built else None
     with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
         _, end_points = inception_v4.inception_v4(
                 x_input, num_classes=self.num_classes, is_training=False,
                 reuse=reuse)
         self.built = True
     output = end_points['Predictions']
     # Strip off the extra reshape op at the output
     probs = output.op.inputs[0]
     return probs
Ejemplo n.º 12
0
    def build_sampler(self):

        images = self.images
        batch_size = tf.shape(images)[0]
        with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
            netOut, _ = inception_v4.inception_v4(images,
                                                  is_training=True,
                                                  dropout_keep_prob=1.0)
        features = tf.reshape(netOut, (batch_size, -1, 1536))

        with tf.variable_scope('Attention'):
            features = self._batch_norm(features,
                                        mode='test',
                                        name='conv_features')
            c, h = self._get_initial_lstm(features=features)
            features_proj = self._project_features(features=features)

            sampled_word_list = []
            alpha_list = []
            beta_list = []
            lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units=self.H)

            for t in range(self.T):
                if t == 0:
                    x = self._word_embedding(
                        inputs=tf.fill([tf.shape(features)[0]], 0))
                else:
                    x = self._word_embedding(inputs=sampled_word, reuse=True)

                context, alpha = self._attention_layer(features,
                                                       features_proj,
                                                       h,
                                                       reuse=(t != 0))
                alpha_list.append(alpha)

                if self.selector:
                    context, beta = self._selector(context, h, reuse=(t != 0))
                    beta_list.append(beta)

                with tf.variable_scope('lstm', reuse=(t != 0)):
                    _, (c, h) = lstm_cell(inputs=tf.concat([x, context], 1),
                                          state=[c, h])

                logits = self._decode_lstm(x, h, context, reuse=(t != 0))
                sampled_word = tf.argmax(logits, 1)
                sampled_word_list.append(sampled_word)

            alphas = tf.transpose(tf.stack(alpha_list), (1, 0, 2))  # (N, T, L)
            # betas = tf.transpose(tf.squeeze(beta_list), (1, 0))    # (N, T)
            sampled_captions = tf.transpose(tf.stack(sampled_word_list),
                                            (1, 0))  # (N, max_len)
        return alphas, sampled_captions
Ejemplo n.º 13
0
    def __call__(self, x_input):
        """Constructs model and return probabilities for given input."""
        reuse = True if self.built else None
        with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
            logits, end_points = inception_v4.inception_v4(
                x_input,
                num_classes=self.num_classes,
                is_training=False,
                reuse=reuse,
                scope=self.scope)

        self.built = True
        return logits, end_points
 def _build(self):
     reuse = True if self.built else None
     with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
         logits, end_points = inception_v4.inception_v4(
                 self.input, num_classes=self.num_classes, is_training=False,
                 reuse=reuse)
         self.built = True
     self.end_points = end_points
     self.logits = logits
     if not self.ckpt_loaded:
         saver = tf.train.Saver()
         print ckpt_dir + 'inception_v4_dir'
         saver.restore(self.sess, ckpt_dir + 'inception_v4.ckpt')
         self.ckpt_loaded = True
 def build_inception(inputs, reuse=True, scope='InceptionV4'):
     is_training = False
     arg_scope = inception_v4_arg_scope(weight_decay=0.0)
     with slim.arg_scope(arg_scope):
         with tf.variable_scope(scope, 'InceptionV4', [inputs],
                                reuse=reuse) as scope:
             with slim.arg_scope([slim.batch_norm, slim.dropout],
                                 is_training=is_training):
                 logits, end_points = inception_v4_base(
                     inputs, final_endpoint='Mixed_5b', scope=scope)
     return [
         end_points['Conv2d_2a_3x3'], end_points['Mixed_4a'],
         end_points['Mixed_5b']
     ]
    def __init__(self, weights_file):
        config = tf.ConfigProto(log_device_placement=False)
        config.gpu_options.allow_growth = True

        self.__graph = tf.Graph()
        self.image_size = inception_v4.inception_v4.default_image_size

        with self.__graph.as_default():
            self.__session = tf.Session(config=config, graph=self.__graph)

            self.images_placeholder = tf.placeholder(
                tf.float32,
                shape=(None, self.image_size, self.image_size, 3),
                name='image')
            self.phase_train_placeholder = tf.placeholder(tf.bool,
                                                          name='phase_train')

            arg_scope = inception_v4.inception_v4_arg_scope()
            with slim.arg_scope(arg_scope):
                logits, end_points = inception_v4.inception_v4(
                    self.images_placeholder,
                    is_training=False,
                    num_classes=1001,
                    create_aux_logits=False)
            for tensor in tf.all_variables():
                print(tensor.name, tensor.shape)

            self.predict = tf.nn.softmax(logits)
            self.end_points = end_points

            # preprocess
            with tf.device('/cpu:0'):
                self.one_image_placeholder = tf.placeholder(tf.float32,
                                                            shape=(None, None,
                                                                   3),
                                                            name='one_image')
                self.preprocessed_image = inception_preprocessing.preprocess_for_eval(
                    self.one_image_placeholder,
                    self.image_size,
                    self.image_size,
                    central_fraction=1)
            print('[{}]:InceptionV4 model restoring...'.format(
                datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
            saver = tf.train.Saver(tf.global_variables(), max_to_keep=3)
            saver.restore(self.__session, weights_file)
            self.__session.graph.finalize()
            print('[{}]:InceptionV4 model restore succeed!'.format(
                datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
Ejemplo n.º 17
0
def build_model():
    """Builds graph for model to train with rewrites for quantization.

  Returns:
    g: Graph with fake quantization ops and batch norm folding suitable for
    training quantized weights.
    train_tensor: Train op for execution during training.
  """
    g = tf.Graph()
    with g.as_default(), tf.device(
            tf.train.replica_device_setter(FLAGS.ps_tasks)):
        inputs, labels, _ = dataset_utils.get_batch(FLAGS.dataset_dir,
                                                    FLAGS.batch_size)
        inputs = tf.image.resize_images(inputs,
                                        [FLAGS.image_size, FLAGS.image_size])

        with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
            logits, _ = inception_v4.inception_v4(
                inputs, is_training=True, num_classes=FLAGS.num_classes)

        labels = slim.one_hot_encoding(labels, FLAGS.num_classes)
        tf.losses.softmax_cross_entropy(labels, logits)

        total_loss = tf.losses.get_total_loss(name='total_loss')
        # Configure the learning rate using an exponential decay.
        num_epochs_per_decay = 2.5
        imagenet_size = 7340
        decay_steps = int(imagenet_size / FLAGS.batch_size *
                          num_epochs_per_decay)

        learning_rate = tf.train.exponential_decay(
            get_learning_rate(),
            tf.train.get_or_create_global_step(),
            decay_steps,
            _LEARNING_RATE_DECAY_FACTOR,
            staircase=True)
        opt = tf.train.RMSPropOptimizer(learning_rate,
                                        momentum=0.9,
                                        epsilon=1.0)

        train_tensor = slim.learning.create_train_op(total_loss, optimizer=opt)

    slim.summaries.add_scalar_summary(total_loss, 'total_loss', 'losses')
    slim.summaries.add_scalar_summary(learning_rate, 'learning_rate',
                                      'training')
    return g, train_tensor
Ejemplo n.º 18
0
def getfeatures(file_name):
    # Extract the features from InceptionNet (The features of Mixed_6a layer)
    slim = tf.contrib.slim
    image_size = inception_v4.inception_v4.default_image_size
    checkpoints_dir = os.getcwd()
    with tf.Graph().as_default():
        image_path = tf.read_file(file_name)
        image = tf.image.decode_jpeg(image_path, channels=3)
        processed_image = inception_preprocessing.preprocess_image(image, image_size, image_size, is_training=False)
        processed_images  = tf.expand_dims(processed_image, 0)
        with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
            vector = inception_v4.inception_v4(processed_images, num_classes=1001, is_training=False)
        init_fn = slim.assign_from_checkpoint_fn(os.path.join(checkpoints_dir, 'inception_v4.ckpt'), slim.get_model_variables('InceptionV4'))
        with tf.Session() as sess:
            init_fn(sess)
            np_image, vector = sess.run([image, vector])
        vector = np.asarray(vector)
        # print vector[1]['Mixed_6a'].shape
        return vector[1]['Mixed_6a']
Ejemplo n.º 19
0
def cnvert_ckpt_to_pb(ckpt_path, output_path):
    g = tf.Graph()
    output_node_names = ['Outputs']
    with g.as_default():
        inputs = tf.placeholder(tf.float32, [None, 256, 256, 3], 'Inputs')
        inputs = tf.image.resize_images(inputs, [299, 299])

        with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
            _, end_points = inception_v4.inception_v4(inputs,
                                                      is_training=False,
                                                      num_classes=6)
            outputs = tf.argmax(end_points['Predictions'], 1, name='Outputs')

        saver = tf.train.Saver()
        with tf.Session() as sess:
            saver.restore(sess, ckpt_path)
            output_graph_def = tf.graph_util.convert_variables_to_constants(
                sess, g.as_graph_def(), output_node_names)
            with tf.gfile.GFile(output_path, "wb") as f:
                f.write(output_graph_def.SerializeToString())
    def __init__(self, weight_file):
        config = tf.ConfigProto(log_device_placement=False)
        config.gpu_options.allow_growth = True

        self.__graph = tf.Graph()
        self.image_size = inception_v4.inception_v4.default_image_size

        with self.__graph.as_default():
            self.__session = tf.Session(config=config, graph=self.__graph)

            self.images_placeholder = tf.placeholder(
                tf.float32,
                shape=(None, self.image_size, self.image_size, 3),
                name='image')
            self.phase_train_placeholder = tf.placeholder(tf.bool,
                                                          name='phase_train')

            arg_scope = inception_v4.inception_v4_arg_scope()
            with slim.arg_scope(arg_scope):
                logits, end_points = inception_v4.inception_v4(
                    self.images_placeholder,
                    is_training=False,
                    num_classes=1001)
            for tensor in tf.all_variables():
                print(tensor.name, tensor.shape)

            self.predict = tf.nn.softmax(logits)
            self.end_points = end_points

            # preprocess
            self.one_image_placeholder = tf.placeholder(tf.float32,
                                                        shape=(None, None, 3),
                                                        name='one_image')
            self.preprocessed_image = inception_preprocessing.preprocess_for_eval(
                self.one_image_placeholder,
                self.image_size,
                self.image_size,
                central_fraction=1)

            saver = tf.train.Saver(tf.global_variables(), max_to_keep=3)
            saver.restore(self.__session, weight_file)
Ejemplo n.º 21
0
def get_bottlenecks(name):
    tf.reset_default_graph()

    inputs, labels = dataset.inputs(name=name, batch_size=1, num_epochs=1, predict=True)
    inputs = tf.reshape(inputs, [1, 299, 299, 3])

    # note about num_classes:
    # we set num_classes=1001 so that the output layer weights are the same size as in the checkpoint
    # otherwise restoring raises an InvalidArgumentError
    # we could of course also use `inception_v4_base` to output the model without the output layer
    # but this way we can still use the final pooling and dropout layers from the original architecture
    # we don't care about the size of num_classes since we are only interested in `PreLogitsFlatten` anyway
    with tf.contrib.slim.arg_scope(inception_v4_arg_scope()):
        _, end_points = inception_v4(
                inputs=inputs,
                num_classes=1001,
                create_aux_logits=False,
                is_training=False,
                dropout_keep_prob=1.0,
                )
    return end_points['PreLogitsFlatten'], labels
Ejemplo n.º 22
0
def test_inception_layer_info():
    """
    print layer name, input tensor and output tensor
    print example:
        Operation:InceptionV4/Logits/Logits/MatMul
        InceptionV4/Logits/Logits/MatMul Input: InceptionV4/Logits/PreLogitsFlatten/flatten/Reshape:0  (?, 1536)
        InceptionV4/Logits/Logits/MatMul Input: InceptionV4/Logits/Logits/weights/read:0  (1536, 1001)
        InceptionV4/Logits/Logits/MatMul Output:InceptionV4/Logits/Logits/MatMul:0


        Operation:InceptionV4/Logits/Logits/BiasAdd
        InceptionV4/Logits/Logits/BiasAdd Input: InceptionV4/Logits/Logits/MatMul:0  (?, 1001)
        InceptionV4/Logits/Logits/BiasAdd Input: InceptionV4/Logits/Logits/biases/read:0  (1001,)
        InceptionV4/Logits/Logits/BiasAdd Output:InceptionV4/Logits/Logits/BiasAdd:0


        Operation:InceptionV4/Logits/Predictions
        InceptionV4/Logits/Predictions Input: InceptionV4/Logits/Logits/BiasAdd:0  (?, 1001)
        InceptionV4/Logits/Predictions Output:InceptionV4/Logits/Predictions:0
    :return:
    """
    image_size = inception_v4.inception_v4.default_image_size
    inputs = tf.placeholder(dtype=tf.float32,
                            shape=(None, image_size, image_size, 3))

    arg_scope = inception_v4.inception_v4_arg_scope()
    with slim.arg_scope(arg_scope):
        logits, end_points = inception_v4.inception_v4(inputs,
                                                       is_training=False,
                                                       num_classes=1001)

    operations = tf.get_default_graph().get_operations()
    for operation in operations:
        print("Operation:{}".format(operation.name))
        for k in operation.inputs:
            print("{} Input: {}  {}".format(operation.name, k.name,
                                            k.get_shape()))
        for k in operation.outputs:
            print("{} Output:{}".format(operation.name, k.name))
        print("\n")
Ejemplo n.º 23
0
def test_inception_variable_info():
    """
    print every variable and its shape in graph
    print example:
        InceptionV4/Mixed_7d/Branch_2/Conv2d_0d_1x3/BatchNorm/moving_variance:0 (256,)
        InceptionV4/Mixed_7d/Branch_2/Conv2d_0e_3x1/weights:0 (3, 1, 512, 256)
        InceptionV4/Mixed_7d/Branch_2/Conv2d_0e_3x1/BatchNorm/beta:0 (256,)
        InceptionV4/Mixed_7d/Branch_2/Conv2d_0e_3x1/BatchNorm/moving_mean:0 (256,)
        InceptionV4/Mixed_7d/Branch_2/Conv2d_0e_3x1/BatchNorm/moving_variance:0 (256,)
        InceptionV4/Mixed_7d/Branch_3/Conv2d_0b_1x1/weights:0 (1, 1, 1536, 256)
        InceptionV4/Mixed_7d/Branch_3/Conv2d_0b_1x1/BatchNorm/beta:0 (256,)
        InceptionV4/Mixed_7d/Branch_3/Conv2d_0b_1x1/BatchNorm/moving_mean:0 (256,)
        InceptionV4/Mixed_7d/Branch_3/Conv2d_0b_1x1/BatchNorm/moving_variance:0 (256,)
        InceptionV4/AuxLogits/Conv2d_1b_1x1/weights:0 (1, 1, 1024, 128)
        InceptionV4/AuxLogits/Conv2d_1b_1x1/BatchNorm/beta:0 (128,)
        InceptionV4/AuxLogits/Conv2d_1b_1x1/BatchNorm/moving_mean:0 (128,)
        InceptionV4/AuxLogits/Conv2d_1b_1x1/BatchNorm/moving_variance:0 (128,)
        InceptionV4/AuxLogits/Conv2d_2a/weights:0 (5, 5, 128, 768)
        InceptionV4/AuxLogits/Conv2d_2a/BatchNorm/beta:0 (768,)
        InceptionV4/AuxLogits/Conv2d_2a/BatchNorm/moving_mean:0 (768,)
        InceptionV4/AuxLogits/Conv2d_2a/BatchNorm/moving_variance:0 (768,)
        InceptionV4/AuxLogits/Aux_logits/weights:0 (768, 1001)
        InceptionV4/AuxLogits/Aux_logits/biases:0 (1001,)
        InceptionV4/Logits/Logits/weights:0 (1536, 1001)
        InceptionV4/Logits/Logits/biases:0 (1001,)
    :return:
    """
    image_size = inception_v4.inception_v4.default_image_size
    inputs = tf.placeholder(dtype=tf.float32,
                            shape=(None, image_size, image_size, 3))

    arg_scope = inception_v4.inception_v4_arg_scope()
    with slim.arg_scope(arg_scope):
        logits, end_points = inception_v4.inception_v4(inputs,
                                                       is_training=False,
                                                       num_classes=1001)
    for tensor in tf.all_variables():
        print(tensor.name, tensor.shape)
Ejemplo n.º 24
0
def build_model():
    """Build the mobilenet_v1 model for evaluation.

  Returns:
    g: graph with rewrites after insertion of quantization ops and batch norm
    folding.
    eval_ops: eval ops for inference.
    variables_to_restore: List of variables to restore from checkpoint.
  """
    g = tf.Graph()
    with g.as_default():
        inputs, labels, _ = dataset_utils.get_batch(FLAGS.dataset_dir,
                                                    FLAGS.batch_size)
        inputs = tf.image.resize_images(inputs,
                                        [FLAGS.image_size, FLAGS.image_size])

        with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
            _, end_points = inception_v4.inception_v4(
                inputs, is_training=False, num_classes=FLAGS.num_classes)

        eval_ops = metrics(end_points['Predictions'], labels)

    return g, eval_ops
def getfeatures(file_name):
    # Extract the features from InceptionNet (The features of Mixed_6a layer)
    slim = tf.contrib.slim
    image_size = inception_v4.inception_v4.default_image_size
    checkpoints_dir = os.getcwd()
    with tf.Graph().as_default():
        image_path = tf.read_file(file_name)
        image = tf.image.decode_jpeg(image_path, channels=3)
        processed_image = inception_preprocessing.preprocess_image(
            image, image_size, image_size, is_training=False)
        processed_images = tf.expand_dims(processed_image, 0)
        with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
            vector = inception_v4.inception_v4(processed_images,
                                               num_classes=1001,
                                               is_training=False)
        init_fn = slim.assign_from_checkpoint_fn(
            os.path.join(checkpoints_dir, 'inception_v4.ckpt'),
            slim.get_model_variables('InceptionV4'))
        with tf.Session() as sess:
            init_fn(sess)
            np_image, vector = sess.run([image, vector])
        vector = np.asarray(vector)
        # print vector[1]['Mixed_6a'].shape
        return vector[1]['Mixed_6a']
Ejemplo n.º 26
0
def build_inception_model(x_input, y_input, reg_input, reuse, is_training,
                          FLAGS):
    dropout = FLAGS.dropout
    batch_size = FLAGS.batch_size
    arg_scope = inception_v4.inception_v4_arg_scope()
    with slim.arg_scope(arg_scope):
        logits, end_points = inception_v4.inception_v4(
            x_input,
            is_training=is_training,
            num_classes=None,
            dropout_keep_prob=dropout,
            reuse=reuse,
            create_aux_logits=False)

    with tf.variable_scope('Beauty', 'BeautyV1', reuse=reuse) as scope:
        # 1 x 1 x 1536
        net = slim.dropout(end_points['global_pool'],
                           dropout,
                           scope='Dropout_1b')
        net = slim.flatten(net, scope='PreLogitsFlatten')
        end_points['PreLogitsFlatten'] = net  # 1536
        # added by Alex Wang for face beauty predict
        # mid_full_conn = slim.fully_connected(end_points['PreLogitsFlatten'],
        #                                      1000, activation_fn=tf.nn.relu,
        #                                      scope='mid_full_conn',
        #                                      trainable=is_training,
        #                                      reuse=reuse)
        # end_points['mid_full_conn'] = mid_full_conn

        predict_conn = slim.fully_connected(end_points['PreLogitsFlatten'],
                                            100,
                                            activation_fn=None,
                                            scope='100_class_conn',
                                            trainable=is_training,
                                            reuse=reuse)

        beauty_weight = tf.convert_to_tensor([[i] for i in range(0, 100)],
                                             dtype=tf.float32)
        regress_conn = tf.matmul(tf.nn.softmax(predict_conn),
                                 beauty_weight)  # 32 * 1

        y_pred_softmax = tf.argmax(predict_conn, 1)
        correct_prediction = tf.equal(y_pred_softmax, y_input)
        acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

        reg_input_reshape = tf.reshape(reg_input, shape=(batch_size, 1))
        # TODO
        diff = tf.subtract(tf.cast(reg_input_reshape, tf.float32),
                           tf.cast(regress_conn, tf.float32))
        cost_rmse = tf.reduce_mean(tf.square(diff), name='cost_rmse')
        # cost_rmse = tf.reduce_mean(tf.exp(tf.abs(diff)), name='cost_rmse')

        ## define cost
        cost_entropy = tf.reduce_mean(
            tf.nn.sparse_softmax_cross_entropy_with_logits(
                labels=y_input, logits=predict_conn, name='cross_entropy'))
        L2 = 0
        for w in tl.layers.get_variables_with_name('InceptionV4', True, True):
            L2 += tf.contrib.layers.l2_regularizer(0.0001)(w)
        for w in tl.layers.get_variables_with_name('Beauty', True, True):
            L2 += tf.contrib.layers.l2_regularizer(0.0001)(w)

        # cost = 10000 * cost_entropy + L2 + 0.001 * cost_rmse
        # cost = 10 * cost_entropy + L2 + 100 * cost_rmse
        # cost = L2 + 0.01 * cost_rmse + 0.01 * cost_entropy
        # TODO
        cost = L2 + 1000 * cost_rmse

        end_points['cost_rmse'] = cost_rmse
        end_points['predict'] = y_pred_softmax
        end_points['regress_conn'] = regress_conn
        end_points['predict_conn'] = predict_conn
        end_points['regress_label'] = diff
        end_points['predict_softmax'] = tf.nn.softmax(predict_conn)
        end_points['beauty_weight'] = beauty_weight
        end_points['acc'] = acc
        end_points['cost_entropy'] = cost_entropy
        end_points['L2'] = L2
        end_points['cost'] = cost

    return end_points
Ejemplo n.º 27
0
    def build_model(self):
        images = self.images
        captions = self.captions
        batch_size = tf.shape(images)[0]

        captions_in = captions[:, :self.T]
        captions_out = captions[:, 1:]

        # do conv distract feature max
        with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
            netOut, _ = inception_v4.inception_v4(images,
                                                  is_training=True,
                                                  dropout_keep_prob=1.0)
        features = tf.reshape(netOut, (batch_size, -1, 1536))

        # batch normalize feature vectors
        with tf.variable_scope('Attention'):
            features = self._batch_norm(features,
                                        mode='train',
                                        name='conv_features')

            c, h = self._get_initial_lstm(features=features)
            x = self._word_embedding(inputs=captions_in)
            features_proj = self._project_features(features=features)

            loss = 0.0
            alpha_list = []
            lstm_cell = tf.nn.rnn_cell.DropoutWrapper(
                tf.nn.rnn_cell.BasicLSTMCell(num_units=self.H),
                output_keep_prob=0.6)
            logitLabels = []

            for t in range(self.T):
                context, alpha = self._attention_layer(features,
                                                       features_proj,
                                                       h,
                                                       reuse=(t != 0))
                alpha_list.append(alpha)

                if self.selector:
                    context, beta = self._selector(context, h, reuse=(t != 0))

                with tf.variable_scope('lstm', reuse=(t != 0)):
                    _, (c,
                        h) = lstm_cell(inputs=tf.concat([x[:, t, :], context],
                                                        1),
                                       state=[c, h])

                logits = self._decode_lstm(x[:, t, :],
                                           h,
                                           context,
                                           dropout=self.dropout,
                                           reuse=(t != 0))
                logitLabels.append(logits)

                loss += tf.reduce_sum(
                    tf.nn.sparse_softmax_cross_entropy_with_logits(
                        labels=captions_out[:, t], logits=logits))

            if self.alpha_c > 0:
                alphas = tf.transpose(tf.stack(alpha_list),
                                      (1, 0, 2))  # (N, T, L)
                alphas_all = tf.reduce_sum(alphas, 1)  # (N, L)
                alpha_reg = self.alpha_c * tf.reduce_sum(
                    (6. / 96 - alphas_all)**2)
                loss += alpha_reg

        return (logitLabels, loss / tf.to_float(batch_size))
 def __init__(self, **kwargs):
     super().__init__('inception_v4.ckpt', 'InceptionV4', \
             inception_v4.inception_v4_arg_scope(), \
             inception_v4.inception_v4, 1, **kwargs)
Ejemplo n.º 29
0
    def __init__(self, weight_file):
        self.image_size = inception_v4.inception_v4.default_image_size
        config = tf.ConfigProto(log_device_placement=False)
        config.gpu_options.allow_growth = True

        self.__graph = tf.Graph()

        with self.__graph.as_default():
            self.__session = tf.Session(config=config, graph=self.__graph)

            self.images_placeholder = tf.placeholder(tf.float32,
                                                     shape=(None, None, 3),
                                                     name='image')
            img_process = inception_preprocessing.preprocess_for_eval(
                self.images_placeholder,
                self.image_size,
                self.image_size,
                central_fraction=1,
                scope='preprocess_test')
            img_process = tf.expand_dims(img_process, 0)

            arg_scope = inception_v4.inception_v4_arg_scope()
            with tf.variable_scope('big', reuse=False) as scope:
                with slim.arg_scope(arg_scope):
                    logits, end_points = inception_v4.inception_v4(
                        img_process,
                        is_training=False,
                        num_classes=1001,
                        dropout_keep_prob=1.0,
                        reuse=False,
                        create_aux_logits=False)

                with tf.variable_scope('Beauty', 'BeautyV1',
                                       reuse=False) as scope:
                    mid_full_conn = slim.fully_connected(
                        end_points['PreLogitsFlatten'],
                        1000,
                        activation_fn=tf.nn.relu,
                        scope='mid_full_conn',
                        trainable=False,
                        reuse=False)

                    predict_conn = slim.fully_connected(mid_full_conn,
                                                        100,
                                                        activation_fn=None,
                                                        scope='100_class_conn',
                                                        trainable=False,
                                                        reuse=False)

                    beauty_weight = tf.convert_to_tensor(
                        [[i] for i in range(0, 100)], dtype=tf.float32)
                    regress_conn = tf.matmul(tf.nn.softmax(predict_conn),
                                             beauty_weight)  # 32 * 1

                    self.end_points = {
                        'mid_full_conn': mid_full_conn,
                        'regress_conn': regress_conn,
                        'predict_conn': predict_conn
                    }

            saver = tf.train.Saver(tf.global_variables(), max_to_keep=3)
            saver.restore(self.__session, weight_file)
Ejemplo n.º 30
0
def main(_):
    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    num_classes = 1001
    itr = 30

    tf.logging.set_verbosity(tf.logging.INFO)

    with tf.Graph().as_default():
        # Prepare graph
        x_input = tf.placeholder(tf.float32, shape=batch_shape)
        img_resize_tensor = tf.placeholder(tf.int32, [2])
        x_input_resize = tf.image.resize_images(
            x_input,
            img_resize_tensor,
            method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)

        shape_tensor = tf.placeholder(tf.int32, [3])
        padded_input = padding_layer_iyswim(x_input_resize, shape_tensor)
        # 330 is the last value to keep 8*8 output, 362 is the last value to keep 9*9 output, stride = 32
        padded_input.set_shape(
            (FLAGS.batch_size, FLAGS.image_resize, FLAGS.image_resize, 3))

        with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
            _, end_points = inception_v4.inception_v4(x_input,
                                                      num_classes=num_classes,
                                                      is_training=False,
                                                      create_aux_logits=True)

        predicted_labels = tf.argmax(end_points['Predictions'], 1)

        # Run computation
        saver = tf.train.Saver(slim.get_model_variables())
        session_creator = tf.train.ChiefSessionCreator(
            scaffold=tf.train.Scaffold(saver=saver),
            checkpoint_filename_with_path=FLAGS.checkpoint_path,
            master=FLAGS.master)

        with tf.train.MonitoredSession(
                session_creator=session_creator) as sess:
            with tf.gfile.Open(FLAGS.output_file, 'w') as out_file:
                for filenames, images in load_images(FLAGS.input_dir,
                                                     batch_shape):
                    final_preds = np.zeros(
                        [FLAGS.batch_size, num_classes, itr])
                    for j in range(itr):
                        if np.random.randint(0, 2, size=1) == 1:
                            images = images[:, :, ::-1, :]
                        resize_shape_ = np.random.randint(310, 331)
                        pred, aux_pred = sess.run(
                            [
                                end_points['Predictions'],
                                end_points['AuxPredictions']
                            ],
                            feed_dict={
                                x_input:
                                images,
                                img_resize_tensor: [resize_shape_] * 2,
                                shape_tensor:
                                np.array([
                                    random.randint(
                                        0, FLAGS.image_resize - resize_shape_),
                                    random.randint(
                                        0, FLAGS.image_resize - resize_shape_),
                                    FLAGS.image_resize
                                ])
                            })
                        final_preds[..., j] = pred + 0.4 * aux_pred
                        #pred = sess.run(end_points['Predictions'],
                        #                                feed_dict={x_input: images, img_resize_tensor: [resize_shape_]*2,
                        #                                           shape_tensor: np.array([random.randint(0, FLAGS.image_resize - resize_shape_), random.randint(0, FLAGS.image_resize - resize_shape_), FLAGS.image_resize])})
                        #labels = sess.run(predicted_labels, feed_dict={x_input: images, img_resize_tensor: [resize_shape_]*2,
                        #shape_tensor: np.array([random.randint(0, FLAGS.image_resize - resize_shape_), random.randint(0, FLAGS.image_resize - resize_shape_), FLAGS.image_resize])})
                        final_preds[..., j] = pred
                    final_probs = np.sum(final_preds, axis=-1)
                    labels = np.argmax(final_probs, 1)
                    for filename, label in zip(filenames, labels):
                        out_file.write('{0},{1}\n'.format(filename, label))
Ejemplo n.º 31
0
image_pixels = 299
classes = 5
flowers_299x299 = "flowers_299x299"

data = "data"
error = "error"
daisy = "daisy"
dandelion = "dandelion"
roses = "roses"
sunflowers = "sunflowers"
tulips = "tulips"

images = tf.placeholder(tf.float32, [None, image_pixels, image_pixels, 3],
                        name="input/x_input")

with slim.arg_scope(inception_v4_arg_scope()):
    logits, end_points = inception_v4(images,
                                      num_classes=classes,
                                      is_training=False)

with tf.Session() as sess:
    ckpt = tf.train.get_checkpoint_state("ckpt")
    if ckpt:
        print(ckpt.model_checkpoint_path)
        tf.train.Saver().restore(sess, ckpt.model_checkpoint_path)
    else:
        raise ValueError("The ckpt file is None.")
    if os.path.exists(os.path.join(data, tulips)):
        shutil.rmtree(os.path.join(data, tulips))
    if not os.path.exists(os.path.join(data, tulips)):
        os.makedirs(os.path.join(data, tulips))
Ejemplo n.º 32
0
def feature_extractor(input_tensor, scope_name="FeatureExtractor"):
    last_layer_name = 'Mixed_5e'
    with tf.contrib.slim.arg_scope(inception_v4.inception_v4_arg_scope()):
        inception_output, end_points = inception_v4.inception_v4_base(
            input_tensor, final_endpoint=last_layer_name, scope=scope_name)
    return inception_output