Exemplo n.º 1
0
class DataLoader(object):
    def __init__(self):
        self.batch_size = settings.BATCH_SIZE
        self.model_contents = Contents().build_model()
        self.model_style = Style()

    def load_data(self):
        y_true_style = self.model_style.predict()
        image_path_list = glob.glob(os.path.join(settings.DATA, '*.jpg'))
        gen = self.train_generator(
            image_path_list,
            y_true_style,
            epochs=10,
        )
        return gen, image_path_list

    def train_generator(self,
                        image_path_list,
                        y_true_style,
                        shuffle=True,
                        epochs=10):
        '''
        Generate train data
        '''
        n_samples = len(image_path_list)
        indices = [i for i in range(n_samples)]
        steps_per_epochs = math.ceil(n_samples / settings.BATCH_SIZE)
        image_path_ndarray = np.array(image_path_list)
        count_epochs = 0
        while True:
            count_epochs += 1
            if shuffle:
                np.random.shuffle(indices)
            for i in range(steps_per_epochs):
                start = settings.BATCH_SIZE * i
                end = settings.BATCH_SIZE * (i + 1)
                X = self.load_images(image_path_ndarray[indices[start:end]])
                batch_size_act = X.shape[0]
                y_true_style_t = [
                    np.repeat(feat, batch_size_act, axis=0)
                    for feat in y_true_style
                ]
                y_true_contents = self.model_contents.predict(X)
                yield X, y_true_style_t + [y_true_contents]
            if epochs is not None:
                if count_epochs >= epochs:
                    raise StopIteration

    def load_images(self, image_path_list):
        '''
        Return batch of array from image_path_list
        '''
        _load_img = lambda x: img_to_array(
            load_img(x, target_size=settings.INPUT_SHAPE[:2]))
        image_list = [
            np.expand_dims(_load_img(path), axis=0) for path in image_path_list
        ]
        return np.concatenate(image_list, axis=0)
Exemplo n.º 2
0
def load_styles(datafile):
    tree = ET.parse(datafile)
    root = tree.getroot()
    for cls in root:
        if (cls.attrib["type"] == "beer"):  # parse only the class of beer
            for cat in cls:
                category = cat.find('name').text
                for subcat in cat:  # subcat is the same as the beer style
                    if (subcat.tag == 'subcategory'):
                        style_name = subcat.find('name').text
                        aroma_node = subcat.find('aroma')
                        aroma = all_text_fragments(aroma_node)
                        appearance = subcat.find('appearance').text
                        flavor = subcat.find('flavor').text
                        mouthfeel = subcat.find('mouthfeel').text
                        impression = subcat.find('impression').text
                        comments = el_find_text(subcat, 'comments', "")
                        ingredients = el_find_text(subcat, 'ingredients', "")
                        examples = subcat.find('examples').text
                        for reading in subcat:
                            for limit in reading:
                                og_min = el_find_text(limit, 'low', 0)
                                og_max = el_find_text(limit, 'high', 99)
                            for limit in reading:
                                fg_min = el_find_text(limit, 'low', 0)
                                fg_max = el_find_text(limit, 'high', 0)
                            for limit in reading:
                                ibu_min = el_find_text(limit, 'low', 0)
                                ibu_max = el_find_text(limit, 'high', 0)
                            for limit in reading:
                                srm_min = el_find_text(limit, 'low', 0)
                                srm_max = el_find_text(limit, 'high', 0)
                            for limit in reading:
                                abv_min = el_find_text(limit, 'low', 0)
                                abv_max = el_find_text(limit, 'high', 0)

                        new_style = Style(style_name=style_name,
                                          category=category,
                                          aroma=aroma,
                                          appearance=appearance,
                                          flavor=flavor,
                                          mouthfeel=mouthfeel,
                                          ingredients=ingredients,
                                          impression=impression,
                                          og_min=og_min,
                                          examples=examples,
                                          comments=comments,
                                          og_max=og_max,
                                          fg_min=fg_min,
                                          fg_max=fg_max,
                                          abv_min=abv_min,
                                          abv_max=abv_max,
                                          srm_min=srm_min,
                                          srm_max=srm_max,
                                          ibu_min=ibu_min,
                                          ibu_max=ibu_max)
                        db.session.add(new_style)
                    db.session.commit()
Exemplo n.º 3
0
 def setUp(self):
     DB.drop_all()
     DB.create_all()
     style = Style(
         '1', 'Pop', 'cool',
         'http://a5.files.biography.com/image/upload/c_fit,cs_srgb,dpr_1.0,h_1200,q_80,w_1200/MTE5NDg0MDU1MTYxNzY3NDM5.jpg'
     )
     DB.session.add(style)
     DB.session.commit()
Exemplo n.º 4
0
 def test_addition_deletion(self):
     """
     Test adding and deleting
     """
     style = Style(
         '2', 'Modern', 'kinda wierd',
         'http://a5.files.biography.com/image/upload/c_fit,cs_srgb,dpr_1.0,h_1200,q_80,w_1200/MTE5NDg0MDU1MTYxNzY3NDM5.jpg'
     )
     DB.session.add(style)
     DB.session.commit()
     self.assertEqual(len(Style.query.all()), 2)
     Style.query.filter_by(name='Modern').delete()
     DB.session.commit()
     self.assertEqual(len(Style.query.all()), 1)
Exemplo n.º 5
0
def response():
    action = cgi_get("action", choices=["edit", "style"])
    data = cgi_get("data", required=False)
    if action == "style":
        style = Style.query().get()
        if not style:
            style = Style()
            style.put()  # for key
        if data:
            for key, val in list(data.items()):
                setattr(style, key, val)
            style.put()
        succeed(style.data())
    if "key" in data:
        entity = db.get(data["key"])
    else:
        entity = PageEdit(**data)
    entity.content = data["content"]
    entity.put()
    succeed(entity.key.urlsafe())
Exemplo n.º 6
0
 def test_style_creation(self):
     print '- test_style_creation'
     style_file = FileStorage(
         stream=open('fast_style_transfer/styles/muse.ckpt'))
     image_file = FileStorage(
         stream=open('fast_style_transfer/styles/muse.jpg'))
     tf_model = TFModel.query.get(1)
     style = Style.create(style_file, image_file, tf_model, 'title',
                          'artist', 'description')
     self.assertEqual(style.style_id, 7)
     self.assertIsInstance(style, Style)
     self.assertEqual(style.title, 'title')
     self.assertEqual(style.artist, 'artist')
     self.assertEqual(style.description, 'description')
     self.assertEqual(style.tf_model_id, 1)
     self.assertIsInstance(style.tf_model, TFModel)
     self.assertEqual(style.image_id, 9)
     self.assertIsInstance(style.image, Image)
     print '+ passed'
Exemplo n.º 7
0
 def __init__(self):
     self.batch_size = settings.BATCH_SIZE
     self.model_contents = Contents().build_model()
     self.model_style = Style()
Exemplo n.º 8
0
    # Insert records into Ingredient
    for row in reader:
        for type in types:
            if row[type] == "TRUE":
                ingredient = Ingredient(name=row['name'])
                ingredient.type = types_dict[type]
                db.add(ingredient)
                db.commit()

with open(file_methods, 'r') as file:
    reader = list(csv.DictReader(file))

    # Insert records into Style
    styles = set()
    for row in reader:
        styles.add(row['style'])
    styles_dict = {}
    for item in styles:
        styles_dict[item] = Style(style=item)
        db.add(styles_dict[item])
    db.commit()

    # Insert records into Method
    for row in reader:
        method = Method(instruction=row['instruction'])
        method.type = types_dict[row['type']]
        method.style = styles_dict[row['style']]
        db.add(method)
        db.commit()

db.close()
Exemplo n.º 9
0
def seed_data(testing=False):
    tf_model = TFModel.create(title='fast_style_transfer',
                              description='Created by Logan Engstrom')

    style_dir = BASEDIR + '/fast_style_transfer/styles/'

    muse_file = FileStorage(stream=open(style_dir + 'muse.ckpt'))
    muse_image = FileStorage(stream=open(style_dir + 'muse.jpg'))
    Style.create(style_file=muse_file, image_file=muse_image,
                 tf_model=tf_model, title='La Muse', artist='Pablo Picasso',
                 description="This painting is also known as Young Woman Drawing and Two Women. It's a story of two women. One sits, copying what she sees in the mirror set up in front of her; the other sleeps with her head in her arms.\nThere was a series of paintings Picasso did at this time of young women drawing, writing or reading. This is Marie Therese Walther not with the rounded, ample forms the painter normally used to depict her but with an angular style. The sleeping girl resembles her as well, and indeed she would be somewhere (anywhere's better than nowhere) in Picasso's affections for some years to come. Maia, their daughter was born a few months after this was painted, in October 1935.")

    rain_file = FileStorage(stream=open(style_dir + 'rain.ckpt'))
    rain_image = FileStorage(stream=open(style_dir + 'rain.jpg'))
    Style.create(style_file=rain_file, image_file=rain_image,
                 tf_model=tf_model, title='Rain Princess',
                 artist='Leonid Afremov',
                 description="Rain Princess is a painting by Leonid Afremov which was uploaded on February 17th, 2014.\nYou can buy this painting from his official shop via this link: https://www.etsy.com/listing/16654120")

    scream_file = FileStorage(stream=open(style_dir + 'scream.ckpt'))
    scream_image = FileStorage(stream=open(style_dir + 'scream.jpg'))
    Style.create(style_file=scream_file, image_file=scream_image,
                 tf_model=tf_model, title='The Scream', artist='Edvard Munch',
                 description="Munch's The Scream is an icon of modern art, the Mona Lisa for our time. As Leonardo da Vinci evoked a Renaissance ideal of serenity and self-control, Munch defined how we see our own age - wracked with anxiety and uncertainty.")

    udnie_file = FileStorage(stream=open(style_dir + 'udnie.ckpt'))
    udnie_image = FileStorage(stream=open(style_dir + 'udnie.jpg'))
    Style.create(style_file=udnie_file, image_file=udnie_image,
                 tf_model=tf_model, title='Udnie', artist='Francis Picabia',
                 description="Udnie was inspired by a dance performance given by the Polish-born actress Stacia Napierkowska on the boat taking Francis Picabia to New York for the Armory Show in 1913. The work combines the decomposition of volumes into planes characteristic of Cubism with the enthusiasm for a world in movement of Italian Futurism. The energy and vitality of the dance find expression in springing arabesque, fragmented coloured planes and the jostling of simplified forms. In the radicalism of its treatment, Udnie marks a decisive step towards the emergence of abstraction in Europe.")

    wave_file = FileStorage(stream=open(style_dir + 'wave.ckpt'))
    wave_image = FileStorage(stream=open(style_dir + 'wave.jpg'))
    Style.create(style_file=wave_file, image_file=wave_image,
                 tf_model=tf_model, title='Under the Wave off Kanagawa',
                 artist='Katsushika Hokusai',
                 description="Katsushika Hokusai's Under the Wave off Kanagawa, also called The Great Wave has became one of the most famous works of art in the world-and debatably the most iconic work of Japanese art. Initially, thousands of copies of this print were quickly produced and sold cheaply. Despite the fact that it was created at a time when Japanese trade was heavily restricted, Hokusai's print displays the influence of Dutch art, and proved to be inspirational for many artists working in Europe later in the nineteenth century.")

    wreck_file = FileStorage(stream=open(style_dir + 'wreck.ckpt'))
    wreck_image = FileStorage(stream=open(style_dir + 'wreck.jpg'))
    Style.create(style_file=wreck_file, image_file=wreck_image,
                 tf_model=tf_model, title='The Shipwreck of the Minotaur',
                 artist='Joseph Mallord William Turner',
                 description="Shipwreck may be regarded as one of the worst things a human being can encounter. The sea is no respecter of persons- instantly, 100s of men can be wiped out. Turner's fascination with man vs. nature is display in The Shipwreck. He wished to portray the power of the elements and how no one is immune from the dangers of an angry sea; he can struggle and fight but ultimately he will be swallowed up by the sea. The unlikelihood of deliverance from such calamity is great.")

    user = User.create(username='******', email='*****@*****.**',
                       password='******')

    user_image = FileStorage(stream=open(
        'fast_style_transfer/source-images/melons.jpg'))
    SourceImage.create(image_file=user_image, user=user,
                       title="Melon - Eden's Gem (Rocky Ford Honeydew)",
                       description="Popular green-flesh muskmelon with a heavily netted rind, and smooth, sweet-flavoured flesh with a complex spicy flavour. This early maturing variety, also known as the Rocky Ford cantaloupe, was developed in 1905 at Rocky Ford, Colorado as a 'crate melon.' Softball-sized 500-g (1-lb) fruit.\nhttps://www.urbanseedling.com/product/melon-edens-gem/")

    if testing:
        image = Image.query.get(1)
        source = SourceImage.query.get(1)
        style = Style.query.get(1)
        StyledImage.create(source_image=source, style=style, testing=testing)
        comment = Comment(user=user, image=image, body="hello world")
        like = Like(user=user, image=image)
        tag = Tag(name='melon')
        image_tag = ImageTag(tag=tag, image=image)

        db.session.add_all([comment, like, tag, image_tag])
    else:
        # create a placeholder image so testing does not
        # write over an actual user's file
        i = Image(file_extension='')
        j = Image(file_extension='')
        db.session.add_all([i, j])

    db.session.commit()