def compress(autoencoder, image_path, compressed_path):
    """
    Represent each of the images as the activities of the narrowest layer
    in the autoencoder.

    autoencoder: the neural network trained on the images
    image_path, compressed_path: str, the directory containing the raw
        uncompressed images, and the directory that will hold the
        compressed images, respectively
    """
    images, imagenames = ldr.load_images(patch_size, image_path)
    for i_image, image in enumerate(images):
        compressed_filename = os.path.join(compressed_path,
                                           imagenames[i_image] + ".pkl")
        compressed_image = None
        n_rows, n_cols = image.shape
        n_patch_rows = int(n_rows / patch_size)
        n_patch_cols = int(n_cols / patch_size)
        for i_row in np.arange(n_patch_rows):
            for i_col in np.arange(n_patch_cols):
                patch = image[i_row * patch_size:(i_row + 1) * patch_size,
                              i_col * patch_size:(i_col + 1) * patch_size]
                compressed_patch = autoencoder.forward_pass(
                    patch,
                    evaluating=True,
                    i_stop_layer=3,
                )
                if compressed_image is None:
                    compressed_image = np.zeros(
                        (n_patch_rows, n_patch_cols, compressed_patch.size))

                compressed_image[i_row, i_col, :] = compressed_patch
        with open(compressed_filename, "wb") as f:
            pkl.dump(compressed_image, f)
Beispiel #2
0
            # Forward pass
            test_outputs = net(inputs)
            test_loss_size = loss(test_outputs, labels.long())
            total_test_loss += test_loss_size.data.item()
        test_loss_hist.append(total_test_loss / len(test_loader))
        test_hist_x.append(idx)
        print("Validation loss = {:.2f}".format(total_test_loss /
                                                len(test_loader)))

    print("Training finished, took {:.2f}s".format(time.time() -
                                                   training_start_time))
    return train_hist_x, train_loss_hist, test_hist_x, test_loss_hist


if __name__ == "__main__":

    dataset = image_loader.load_images("dataset1", (image_h, image_w))

    dataset_train, dataset_test = sklearn.model_selection.train_test_split(
        dataset, test_size=0.25)

    train_loader = make_loader(dataset_train, 32)
    test_loader = make_loader(dataset_test, 32)

    device = "cpu"  # Change to "cuda" if on a machine with cuda
    net = cnn()
    net.to(device)

    train_hist_x, train_loss_hist, test_hist_x, test_loss_hist = train_model(
        net, 10, 1e-2, train_loader, test_loader)
Beispiel #3
0
"""
Created on Sun Mar 26 14:13:45 2017

@author: phuem
"""

#http://scikit-learn.org/stable/modules/svm.html

from sklearn import svm, metrics
import image_loader
import numpy as np
import matplotlib.pyplot as plt

#------------------------------------------------------------------------------
#Load and reshape Images with image_loader.py
training_set, training_labels = image_loader.load_images("images/train/")
test_set, test_labels = image_loader.load_images("images/test/")

resize_set, resize_labels = image_loader.prep_datas(training_set,
                                                    training_labels)
resize_test_set, resize_test_labels = image_loader.prep_datas(
    test_set, test_labels)

#---------------------------------------------------------------
#very ugly way to bring vectors to the right shape for SVC fit()
a = []
for x in resize_set:
    a.append(x.tolist())
#----------------------------------------------------------------
X = a  #reshaped images (training)
y = resize_labels  #labels
Beispiel #4
0
import sys
import tensorflow as tf
import numpy as np
from matplotlib import patches as patches
from matplotlib import pyplot as plt

from alexnet import AlexNet
from image_loader import load_images

files = sys.argv[1:]
imgs = load_images(files)

m, h, w, _ = imgs.shape

# load the saved tensorflow model and evaluate a list of paths to PNG files (must be 150x150)
num_classes = 1

X = tf.placeholder(tf.float32, shape=(m, h, w, 1))
Y = tf.placeholder(tf.float32, shape=(m, num_classes))
dropout = tf.placeholder(tf.float32)

model = AlexNet(X, dropout, num_classes)

predictions = model.logits > 0

saver = tf.train.Saver()

with tf.Session() as sess:
    saver.restore(sess, "./tensorflow-ckpt/model.ckpt")
    logits = sess.run(model.logits, feed_dict={X: imgs, dropout: 0})
    _, y_h, y_w, __ = logits.shape
Beispiel #5
0
import sys
import tensorflow as tf

from alexnet import AlexNet
from image_loader import load_images

# load the saved tensorflow model and evaluate a list of paths to PNG files (must be 150x150)
num_classes = 1

X = tf.placeholder(tf.float32, shape=(None, 150, 150, 1))
Y = tf.placeholder(tf.float32, shape=(None, num_classes))
dropout = tf.placeholder(tf.float32)

model = AlexNet(X, dropout, num_classes)

predictions = model.logits > 0

saver = tf.train.Saver()

files = sys.argv[1:]
imgs = load_images(files, resize=True, remove_alpha=True)

with tf.Session() as sess:
    saver.restore(sess, "./tensorflow-ckpt/model.ckpt")
    preds = sess.run(predictions, feed_dict={X: imgs, dropout: 0})
    print([
        str(i) + ": " + ("molecule" if pred else "non-molecule")
        for i, pred in enumerate(preds)
    ])
Beispiel #6
0
X = tf.placeholder(tf.float32, shape=(None, 150, 150, 1))
dropout = tf.constant(0)

model = AlexNet(X, dropout, num_classes)

graph = tf.get_default_graph()
# from our graph, get the tensors for our hidden layers
first_hidden_layer_tensor = graph.get_tensor_by_name("conv1/Relu:0")
second_hidden_layer_tensor = graph.get_tensor_by_name("conv2/Relu:0")
third_hidden_layer_tensor = graph.get_tensor_by_name("conv3/Relu:0")

saver = tf.train.Saver()

imgs = load_images([
    "./positives/images/*.png",
    "./positives/manually-collected/*.png"
    ])

with tf.Session() as sess:
    saver.restore(sess, "./tensorflow-ckpt/model.ckpt")

    first_hidden_layer, second_hidden_layer, third_hidden_layer = sess.run((first_hidden_layer_tensor,
                                                                            second_hidden_layer_tensor,
                                                                            third_hidden_layer_tensor),
                                                                           feed_dict={X: imgs})

    def deconvolve_layer_1(rng, ignore_pad=False):
        return deconvolve_range(rng, filt_size=7, stride=4)

    def deconvolve_layer_2(rng, ignore_pad=False):
        patch_range = deconvolve_range(rng, filt_size=5, stride=1, pad=(0 if ignore_pad else 2))
Beispiel #7
0
def run(program_root):
    lgr.debug("Script start")
    with ConfigEnvironment(conf):
        images = load_images()
        if images is None or len(images) == 0:
            return
        # Check if the images are valid
        res = reduce(lambda a, b: a and b.is_valid, images, True)
        if not res:
            lgr.debug("Find invalid image name.")
            show_message_box(
                "发现不正确的图片名称,注意图片名称应当符合“名字_装裱_作者_朝代.jpg”(或png)的格式,且请注意"
                "附上封面,封面的名称应该为“名字_装裱_作者_朝代_cover.jpg”(或者png)的格式。")
            return

        if not need_recreate_motion_xml(images):
            lgr.debug("No update is found.")
            return
        else:
            daily_image_dir = \
                prepare_image_directories(program_root)
            if daily_image_dir is None:
                lgr.debug("Fail to create destination folders")
                show_message_box("无法创建目标文件夹")
                return
            # copy images into this dir
            lgr.debug("Copying images into destination folder")
            for image in images:
                src_abs_path = image.image_path
                dst_abs_path = os.path.join(daily_image_dir, image.image_name)
                lgr.debug("Copying %s" % image.image_name)
                shutil.copy(src_abs_path, dst_abs_path)

                # copy cover if exits
                src_cover_abs_path = image.get_cover_path()
                if os.path.exists(src_cover_abs_path):
                    dst_cover_abs_path = os.path.join(
                        daily_image_dir,
                        os.path.basename(image.get_cover_path()))
                    lgr.debug("Find cover and copying %s" %
                              os.path.basename(image.get_cover_path()))
                    shutil.copy(src_cover_abs_path, dst_cover_abs_path)

                image.move_to(daily_image_dir)

                # generate thumbnail
                lgr.debug("Creating thumbnail")
                build_thumbnail(image)

            lgr.debug("Done copying.")
            lgr.debug("Creating motion.xml file")

            # create motion file
            create_motion_xml(images, program_root)
            # copy motion file into conf folder of the WPF program
            lgr.debug("Moving motion.xml to WPF program folder")
            conf_dir = os.path.join(program_root, "conf")
            dst_motion_path = os.path.join(conf_dir, "motion.xml")
            if os.path.exists(dst_motion_path):
                os.remove(dst_motion_path)
            shutil.copy(get_motion_xml_path(program_root), dst_motion_path)

            # update conf and save history
            conf.update_history(map(lambda aw: aw.artwork_full_name, images))

            lgr.debug("Done!\n\n\n")
            show_message_box("完成!")