コード例 #1
0
def _parse_ucf_features_test(record):
    features = {
        "img": tf.FixedLenFeature((), tf.string, default_value=''),
        "label": tf.FixedLenFeature((), tf.float32, default_value=0),
        "score": tf.FixedLenFeature((), tf.int64, default_value=0),
        "width": tf.FixedLenFeature((), tf.int64, default_value=0),
        "height": tf.FixedLenFeature((), tf.int64, default_value=0),
        "channel": tf.FixedLenFeature((), tf.int64, default_value=0)
    }
    parsed_features = tf.parse_single_example(record, features)
    # for key in parsed_features:
    #     print(key, type(parsed_features[key]))

    # print(type(parsed_features['img']))
    img = tf.decode_raw(parsed_features['img'], tf.float32)
    img_reshape = tf.reshape(img, (tf.stack([
        parsed_features['width'], parsed_features['height'],
        parsed_features['channel']
    ])))
    img_reshape = inception_preprocessing.preprocess_for_eval(
        img_reshape,
        image_size,
        image_size,
        central_fraction=1,
        scope='preprocess_test')

    return img_reshape, parsed_features['width'], parsed_features['height'], parsed_features['channel'], \
           parsed_features['score'] * 5, parsed_features['label'] * 20
コード例 #2
0
def _parse_ucf_features_train(record):
    features = {"img": tf.FixedLenFeature((), tf.string, default_value=''),
                "label": tf.FixedLenFeature((), tf.float32, default_value=0),
                "score": tf.FixedLenFeature((), tf.int64, default_value=0),
                "width": tf.FixedLenFeature((), tf.int64, default_value=0),
                "height": tf.FixedLenFeature((), tf.int64, default_value=0),
                "channel": tf.FixedLenFeature((), tf.int64, default_value=0)}
    parsed_features = tf.parse_single_example(record, features)

    img = tf.decode_raw(parsed_features['img'], tf.float32)
    img_reshape = tf.reshape(img, (
        tf.stack([parsed_features['width'], parsed_features['height'], parsed_features['channel']])))

    img_reshape = tf.image.random_flip_left_right(img_reshape)
    img_reshape = tf.image.random_brightness(img_reshape, max_delta=10. / 255.)
    img_reshape = tf.image.random_saturation(img_reshape, lower=0.9, upper=1.1)
    angles = tf.random_uniform(shape=(1,), minval=-0.2, maxval=0.2,
                               dtype=tf.float32,
                               seed=tf.set_random_seed(1234), name=None)

    noise = tf.random_normal(shape=tf.shape(img_reshape), mean=0.0, stddev=0.01, dtype=tf.float32)
    img_reshape = img_reshape + noise

    img_reshape = tf.contrib.image.rotate(tf.expand_dims(img_reshape, 0), angles)
    img_reshape = inception_preprocessing.preprocess_for_eval(
        tf.squeeze(img_reshape), image_size, image_size,
        central_fraction=0.9, scope='preprocess_train')

    return img_reshape, parsed_features['width'], parsed_features['height'], parsed_features['channel'], \
           parsed_features['score'] * 5, parsed_features['label'] * 20
コード例 #3
0
def preprocess_image(image_buffer,
                     output_height,
                     output_width,
                     num_channels=_NUM_CHANNELS,
                     is_training=False):
    if is_training:
        return preprocess_for_train(image_buffer, output_height, output_width,
                                    num_channels)
    else:
        return preprocess_for_eval(image_buffer, output_height, output_width,
                                   num_channels)
コード例 #4
0
def build_model(graph):
    with graph.as_default():
        with tf.contrib.slim.arg_scope(resnet.inception_resnet_v2_arg_scope()):
            placeholder = tf.placeholder(tf.string, name='input')
            image = tf.image.decode_jpeg(placeholder, channels=3, name='image')
            image = preprocess.preprocess_for_eval(image, FLAGS.image_size,
                                                   FLAGS.image_size)
            image = tf.expand_dims(image, 0)
            logits, end_points = resnet.inception_resnet_v2(
                image, is_training=False, dropout_keep_prob=1.0)
        saver = tf.train.Saver(tf.global_variables())
    return placeholder, logits, end_points
コード例 #5
0
    def getImage(self, expandDim):
        if( self.inputH is None ):
            raise ValueError("input height not set")
        if( self.inputC is not 3):
            raise ValueError("Only ch=3 implemented so far")
        if( self.imageUrl is None):
            raise ValueError("Image URL is not set")
        if( self.inputH != self.inputW):
            raise ValueError("Non-square inputs not yet implemented")

        try:
            f = urllib.request.urlopen(self.imageUrl)
            jpeg_str = f.read()
            image = Image.open(BytesIO(jpeg_str))
        except IOError:
            raise ValueError('Cannot retrieve image. Please check url: ' + self.imageUrl)

        image = np.asarray(image.convert('RGB'))
        print("image shape: ", image.shape)
        m = min(image.shape[0], image.shape[1])
        print("min: ", m)
        if(self.preprocessingType == "vgg"):
            image = vgg_preprocessing.preprocess_for_eval(image, self.inputH, self.inputW, m)
        elif(self.preprocessingType == "inception"):
            # image = image.astype("float32")
            # with tf.Session() as sess:
            #     image = image.eval(session=sess)
            image = tf.convert_to_tensor(image)
            image = inception_preprocessing.preprocess_for_eval(image, height=self.inputH, width=self.inputW)
        elif(self.preprocessingType == "resize_only"):
            image = np.expand_dims(image, 0)
            image = tf.image.resize_bilinear(image, [self.inputH, self.inputW], align_corners=False)
            image = tf.squeeze(image, axis=0)
            # image = tf.convert_to_tensor(image)
        else:
            raise ValueError("Unknown preprocessing type: ", self.preprocessingType)
        # image = tf.expand_dims(image, 0)
        print("new shape: ", image.shape)
        print("new type: ", type(image))

        with tf.Session() as sess:
            image = image.eval(session=sess)
        # print("new shape2: ", image.shape)
        # print("new type2: ", type(image))

        if(len(image.shape) == 3 and expandDim):
            image = np.expand_dims(image, 0)

        print("new shape3: ", image.shape)
        print("new type3: ", type(image))

        return image
コード例 #6
0
    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")))
コード例 #7
0
    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)
コード例 #8
0
def preprocess_frame(frame_path, gif_path, train_mode):
    print("function: preprocess_frame()")
    if train_mode == True:
        gif_path = os.path.join(train_path, gif_path)
    else:
        gif_path = os.path.join(test_path, gif_path)
    frame_path = os.path.join(gif_path, frame_path)
    frame_contents = tf.read_file(frame_path)
    print(frame_contents)
    frame_tensor = tf.image.decode_png(frame_contents)
    print(frame_tensor)
    # print input tensor shape??
    print("frame tensor shape:", frame_tensor.get_shape())
    # print tensor everytime it's modified
    #	resized_frame = tf.image.resize_images(frame_tensor,299,299)
    frame_tensor = tf.reshape(frame_tensor, (299, 299, 3))
    #	frame_tensor = 2*(frame_tensor/tf.cast(255.0,tf.uint8))-tf.cast(1.0,tf.uint8)
    frame_tensor = preprocess_for_eval(frame_tensor, height=227, width=227)
    #	frame_tensor = tf.cast(frame_tensor, uint8)

    return frame_tensor
コード例 #9
0
def load_model():
    tf.reset_default_graph()
    slim = tf.contrib.slim
    height = 299
    width = 299
    channels = 3
    X = tf.placeholder(shape=[None, None, None, 3], dtype=tf.float32)
    X_processed = tf.map_fn(
        lambda x: pre.preprocess_for_eval(image=x, width=width, height=height),
        X)

    with slim.arg_scope(inception_resnet_v2_arg_scope()):
        inception_resnet_v2(X_processed, num_classes=1001, is_training=False)

    saver = tf.train.Saver()
    sess = tf.Session()
    saver.restore(sess, "inception_resnet_v2_2016_08_30.ckpt")
    graph = sess.graph
    mid_layer = graph.get_tensor_by_name(
        "InceptionResnetV2/AuxLogits/Conv2d_2a_5x5/BatchNorm/FusedBatchNorm:0")
    mid_layer_a = tf.contrib.layers.flatten(mid_layer)

    return (sess, X, mid_layer_a)
コード例 #10
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)