Пример #1
0
def load_logo(data_dir):
    image_files = os.listdir(data_dir)
    dataset = np.ndarray(
        shape=(len(image_files), common.CNN_IN_HEIGHT, common.CNN_IN_WIDTH,
               common.CNN_IN_CH),
        dtype=np.float32)
    print(data_dir)
    num_images = 0
    for image in image_files:
        image_file = os.path.join(data_dir, image)
        try:
            if common.CNN_IN_CH == 1:
                image_data = skimage.io.imread(image_file, as_grey=True)
                image_data = image_data.reshape(common.CNN_IN_HEIGHT,
                                                common.CNN_IN_WIDTH,
                                                common.CNN_IN_CH)
            else:
                image_data = skimage.io.imread(image_file)
            image_data = preprocess.scaling(image_data)
            if image_data.shape != (common.CNN_IN_HEIGHT, common.CNN_IN_WIDTH,
                                    common.CNN_IN_CH):
                raise Exception(
                    'Unexpected image shape: %s' % str(image_data.shape))
            dataset[num_images, :, :] = image_data
            num_images = num_images + 1
        except IOError as e:
            print('Could not read:', image_file, ':', e,
                  '-it\'s ok, skipping.')

    dataset = dataset[0:num_images, :, :]
    print('Full dataset tensor:', dataset.shape)
    print('Mean:', np.mean(dataset))
    print('Standard deviation:', np.std(dataset))
    return dataset
Пример #2
0
def logo_recognition(sess, img, obj_proposal, graph_params):
    # recognition results
    recog_results = {}
    recog_results['obj_proposal'] = obj_proposal

    # Resize image
    if img.shape != common.CNN_SHAPE:
        img = imresize(img, common.CNN_SHAPE, interp='bicubic')

    # Pre-processing
    img = preprocess.scaling(img)
    img = img.reshape((1, common.CNN_IN_HEIGHT, common.CNN_IN_WIDTH,
                       common.CNN_IN_CH)).astype(np.float32)

    # Logo recognition
    pred = sess.run([graph_params['pred']],
                    feed_dict={graph_params['target_image']: img})
    recog_results['pred_class'] = common.CLASS_NAME[np.argmax(pred)]
    recog_results['pred_prob'] = np.max(pred)

    return recog_results
Пример #3
0
def main():
    if len(sys.argv) > 1:
        test_image_fn = sys.argv[1]
        if not os.path.exists(test_image_fn):
            print("Not found:", test_image_fn)
            sys.exit(-1)
    else:
        # Select a test image from a test directory
        test_dirs = [
            os.path.join(common.CROPPED_AUG_IMAGE_DIR, class_name, 'test')
            for class_name in common.CLASS_NAME
        ]
        test_dir = np.random.choice(test_dirs)
        test_images_fn = [test_image for test_image in os.listdir(test_dir)]
        test_image_fn = np.random.choice(test_images_fn, 1)[0]
        test_image_fn = os.path.join(test_dir, test_image_fn)
    print("Test image:", test_image_fn)

    # Open and resize a test image
    if common.CNN_IN_CH == 1:
        test_image_org = skimage.io.imread(test_image_fn, as_grey=True)
        test_image_org = test_image_org.reshape(
            common.CNN_IN_HEIGHT, common.CNN_IN_WIDTH, common.CNN_IN_CH)
    else:
        test_image_org = skimage.io.imread(test_image_fn)
    if test_image_org.shape != (common.CNN_IN_HEIGHT, common.CNN_IN_WIDTH,
                                common.CNN_IN_CH):
        test_image_org = imresize(
            test_image_org, (common.CNN_IN_HEIGHT, common.CNN_IN_WIDTH),
            interp='bicubic')
    test_image_org = preprocess.scaling(test_image_org)
    test_image = test_image_org.reshape(
        (1, common.CNN_IN_HEIGHT, common.CNN_IN_WIDTH,
         common.CNN_IN_CH)).astype(np.float32)

    # Training model
    graph = tf.Graph()
    with graph.as_default():
        # Weights and biases
        model_params = model.params()

        # restore weights
        f = "weights.npz"
        if os.path.exists(f):
            initial_weights = load_initial_weights(f)
        else:
            initial_weights = None

        if initial_weights is not None:
            assert len(initial_weights) == len(model_params)
            assign_ops = [
                w.assign(v) for w, v in zip(model_params, initial_weights)
            ]

        # A placeholder for a test image
        tf_test_image = tf.constant(test_image)

        # model
        logits = model.cnn(tf_test_image, model_params, keep_prob=1.0)
        test_pred = tf.nn.softmax(logits)

        # Restore ops
        saver = tf.train.Saver()

    # Recognize a brand logo of test image
    with tf.Session(graph=graph) as session:
        tf.global_variables_initializer().run()
        if initial_weights is not None:
            session.run(assign_ops)
            print('initialized by pre-learned weights')
        elif os.path.exists("models"):
            save_path = "models/deep_logo_model"
            saver.restore(session, save_path)
            print('Model restored')
        else:
            print('initialized')
        pred = session.run([test_pred])
        print("Class name:", common.CLASS_NAME[np.argmax(pred)])
        print("Probability:", np.max(pred))
def main():
    if len(sys.argv) > 1:
        test_image_fn = sys.argv[1]
        if not os.path.exists(test_image_fn):
            print("Not found:", test_image_fn)
            sys.exit(-1)
    else:
        # Select a test image from a test directory
        test_dirs = [
            os.path.join(common.CROPPED_AUG_IMAGE_DIR, class_name, 'test')
            for class_name in common.CLASS_NAME
        ]
        test_dir = np.random.choice(test_dirs)
        test_images_fn = [test_image for test_image in os.listdir(test_dir)]
        test_image_fn = np.random.choice(test_images_fn, 1)[0]
        test_image_fn = os.path.join(test_dir, test_image_fn)
    print("Test image:", test_image_fn)

    # Open and resize a test image
    if common.CNN_IN_CH == 1:
        test_image_org = skimage.io.imread(test_image_fn, as_grey=True)
        test_image_org = test_image_org.reshape(common.CNN_IN_HEIGHT,
                                                common.CNN_IN_WIDTH,
                                                common.CNN_IN_CH)
    else:
        test_image_org = skimage.io.imread(test_image_fn)
    if test_image_org.shape != (common.CNN_IN_HEIGHT, common.CNN_IN_WIDTH,
                                common.CNN_IN_CH):
        test_image_org = imresize(test_image_org,
                                  (common.CNN_IN_HEIGHT, common.CNN_IN_WIDTH),
                                  interp='bicubic')
    test_image_org = preprocess.scaling(test_image_org)
    test_image = test_image_org.reshape(
        (1, common.CNN_IN_HEIGHT, common.CNN_IN_WIDTH,
         common.CNN_IN_CH)).astype(np.float32)

    # Training model
    graph = tf.Graph()
    with graph.as_default():
        # Weights and biases
        model_params = model.params()

        # restore weights
        f = "weights.npz"
        if os.path.exists(f):
            initial_weights = load_initial_weights(f)
        else:
            initial_weights = None

        if initial_weights is not None:
            assert len(initial_weights) == len(model_params)
            assign_ops = [
                w.assign(v) for w, v in zip(model_params, initial_weights)
            ]

        # A placeholder for a test image
        tf_test_image = tf.constant(test_image)

        # model
        logits = model.cnn(tf_test_image, model_params, keep_prob=1.0)
        test_pred = tf.nn.softmax(logits)

        # Restore ops
        saver = tf.train.Saver()

    # Recognize a brand logo of test image
    with tf.Session(graph=graph) as session:
        tf.global_variables_initializer().run()
        if initial_weights is not None:
            session.run(assign_ops)
            print('initialized by pre-learned weights')
        elif os.path.exists("models"):
            save_path = "models/deep_logo_model"
            saver.restore(session, save_path)
            print('Model restored')
        else:
            print('initialized')
        pred = session.run([test_pred])
        print("Class name:", common.CLASS_NAME[np.argmax(pred)])
        print("Probability:", np.max(pred))