def main():
    args = parser.parse_args()
    log_dir = os.path.join(args.output_path, "log")
    checkpoint_dir = os.path.join(args.output_path, "model")
    maybe_make_dir(log_dir)
    maybe_make_dir(checkpoint_dir)

    model = ImageModel()
    model.train(image_path=args.image_path,
                num_epochs=args.num_epochs,
                log_dir=log_dir,
                checkpoint_dir=checkpoint_dir)
def main():
    args = parser.parse_args()
    log_dir = os.path.join(args.output_path, "log")
    checkpoint_dir = os.path.join(args.output_path, "model")
    maybe_make_dir(log_dir)
    maybe_make_dir(checkpoint_dir)

    model = ImageModel()
    model.train(
        image_path=args.image_path,
        num_epochs=args.num_epochs,
        log_dir=log_dir,
        checkpoint_dir=checkpoint_dir)
Ejemplo n.º 3
0
def run():
    name = 'colored-mnist'
    epochs = 200
    subdir = name + "-" + time.strftime("%Y%m%d-%H%M%S")
    if not os.path.isdir(subdir):
        os.mkdir(subdir)
    

    bs = 150
    data_train = CaptionedMNIST(banned=[np.random.randint(0,10) for i in xrange(12)], dataset='train', num=50000, bs=bs)
    data_valid = CaptionedMNIST(banned=[np.random.randint(0,10) for i in xrange(12)], dataset='valid', num=10000, bs=bs)

    train_stream = DataStream.default_stream(data_train, iteration_scheme=SequentialScheme(data_train.num_examples, bs))
    valid_stream = DataStream.default_stream(data_valid, iteration_scheme=SequentialScheme(data_valid.num_examples, bs))


    img_height, img_width = (60,60)

    
    x = T.matrix('features')
    #x.tag.test_value = np.random.rand(bs, 60*60).astype('float32')
    y = T.lmatrix('captions')
    #y.tag.test_value = np.random.rand(bs, 12).astype(int)
    mask = T.lmatrix('mask')
    #mask.tag.test_value = np.ones((bs,12)).astype(int)

    K = 29
    lang_N = 14
    N = 32
    read_size = 8
    write_size = 8
    m = 256
    gen_dim = 300
    infer_dim = 300
    z_dim = 150
    l = 512

    model = ImageModel(bs, K, lang_N, N, read_size, write_size, m, gen_dim, infer_dim, z_dim, l, image_size=60*60, cinit=-10, channels=3)
    model._inputs = [x,y,mask]

    kl, log_recons, log_likelihood, c = model.train(x,y,mask)
    kl.name = 'kl'
    log_recons.name = 'log_recons'
    log_likelihood.name = 'log_likelihood'
    c.name = 'c'

    model._outputs = [kl, log_recons, log_likelihood, c]

    params = model.params

    from solvers.RMSProp import RMSProp as solver
    lr = theano.shared(np.asarray(0.001).astype(theano.config.floatX))
    updates = solver(log_likelihood, params, lr=lr)#0.001)#, clipnorm=10.0)
    model._updates = updates

    logger.info('Compiling sample function')
    model.build_sample_function(y, mask)
    logger.info('Compiled sample function')

    # ============= TRAIN =========
    plots = [['train_kl','valid_kl'],
             ['train_log_recons','valid_log_recons'],
             ['train_log_likelihood','valid_log_likelihood']]
    main_loop = MainLoop(model, train_stream,
                         [FinishAfter(epochs),
                          Track(variables=['kl','log_recons','log_likelihood'], prefix='train'),
                          #TrackBest(variables=['kl'], prefix='train'),
                          DataStreamTrack(valid_stream, ['kl','log_recons','log_likelihood'], prefix='valid'),
                          SampleSentences(subdir, bs, 60, 60),
                          DropLearningRate(lr, 110, 0.00001),
                          Plot(name, plots, 'http://nameless-wave-6526.herokuapp.com/'),
                          SaveModel(subdir, name+'.model'),
                          TimeProfile(),
                          Printing()])
    main_loop.run()
Ejemplo n.º 4
0
def run():
    name = 'coco-nopeep'
    epochs = 200
    subdir = name + "-" + time.strftime("%Y%m%d-%H%M%S")
    if not os.path.isdir(subdir):
        os.mkdir(subdir)
    

    bs = 200
    data_train = MSCoco(dataset='train', num=82611, bs=bs)
    data_valid = MSCoco(dataset='val', num=4989, bs=bs)

    train_stream = DataStream.default_stream(data_train, iteration_scheme=SequentialScheme(data_train.num_examples, bs))
    valid_stream = DataStream.default_stream(data_valid, iteration_scheme=SequentialScheme(data_valid.num_examples, bs))


    img_height, img_width = (32,32)

    
    x = T.matrix('features')
    #x.tag.test_value = np.random.rand(bs, 3*32*32).astype('float32')
    y = T.lmatrix('captions')
    #y.tag.test_value = np.random.rand(bs, 57).astype(int)
    mask = T.lmatrix('mask')
    #mask.tag.test_value = np.ones((bs,57)).astype(int)

    K = 25323
    lang_N = 57
    N = 32
    read_size = 9
    write_size = 9
    m = 256
    gen_dim = 550
    infer_dim = 550
    z_dim = 275
    l = 512

    model = ImageModel(bs, K, lang_N, N, read_size, write_size, m, gen_dim, infer_dim, z_dim, l, image_size=32*32, channels=3, cinit=0.0)
    model._inputs = [x,y,mask]

    kl, log_recons, log_likelihood, c = model.train(x,y,mask)
    kl.name = 'kl'
    log_recons.name = 'log_recons'
    log_likelihood.name = 'log_likelihood'
    c.name = 'c'

    model._outputs = [kl, log_recons, log_likelihood, c]

    params = model.params

    #from solvers.RMSProp import RMSProp as solver
    lr = theano.shared(np.asarray(0.001).astype(theano.config.floatX))
    #updates = solver(log_likelihood, params, lr=lr, clipnorm=10.0)#0.001)
    #lr = 0.001
    grads = T.grad(log_likelihood, params)
    his = []
    for p in params:
        pz = p.get_value()*0
        his.append(theano.shared(pz))

    threshold = 10.0
    decay = 0.9
    updates = OrderedDict()

    for p, ph, g in zip(params, his, grads):
        l2_norm = T.sqrt(T.sqr(g).sum())
        m = T.switch(l2_norm < threshold, 1, threshold/l2_norm)
        grad = m*g
        
        ph_n = decay * ph + (1-decay)*grad**2
        updates[ph] = ph_n
        updates[p] = p-(lr/T.sqrt(ph_n+1e-6))*grad
    
    model._updates = updates

    logger.info('Compiling sample function')
    model.build_sample_function(y, mask)
    logger.info('Compiled sample function')

    # ============= TRAIN =========
    plots = [['train_kl','valid_kl'],
             ['train_log_recons','valid_log_recons'],
             ['train_log_likelihood','valid_log_likelihood']]
    main_loop = MainLoop(model, train_stream,
                         [FinishAfter(epochs),
                          Track(variables=['kl','log_recons','log_likelihood'], prefix='train'),
                          #TrackBest(variables=['kl'], prefix='train'),
                          DataStreamTrack(valid_stream, ['kl','log_recons','log_likelihood'], prefix='valid'),
                          SampleSentences(subdir, bs, 32, 32),
                          DropLearningRate(lr, 25, 0.0001),
                          Plot(name, plots, 'http://nameless-wave-6526.herokuapp.com/'),
                          SaveModel(subdir, name+'.model'),
                          TimeProfile(),
                          Printing()])
    main_loop.run()
Ejemplo n.º 5
0
import sys
import os
from image_model import ImageModel

model = ImageModel(batch_size=12, epochs=24, image_size=48, n=4, version=1)
model.prepare_train_data()
model.train()
# model.load("image_model.h5")

input_dir = "data/test"

for idx, category in enumerate(model.categories):
    category_dir = input_dir + "/" + category

    for file in os.listdir(category_dir):
        if file != ".DS_Store":
            filepath = category_dir + "/" + file
            result = model.predict(filepath)
            if result == category:
                print("[OK] " + result + ": ", filepath)
            else:
                print("[NG] " + result + ": ", filepath)

model.evaluate()
Ejemplo n.º 6
0
    def get_caption(self, image_name):
        image_model = ImageModel(image_name)
        caption = image_model.get_pred()

        return caption
Ejemplo n.º 7
0
    def get_caption(self, path):
        image_model = ImageModel(path)
        caption = image_model.get_pred()

        return caption
Ejemplo n.º 8
0
from flask import Flask, request, redirect, url_for
from flask import send_from_directory, render_template
from werkzeug.utils import secure_filename
from image_model import ImageModel
from datetime import datetime

sys.path.append(os.curdir)  # カレントファイルをインポートするための設定
UPLOAD_FOLDER = '/tmp/uploads'

os.makedirs(UPLOAD_FOLDER, exist_ok=True)
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])

app = Flask(__name__, static_url_path='/static', static_folder='assets/static')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

model = ImageModel()
model.load('image_model.h5')

model.prepare_train_data()


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS


@app.route('/flask/uploader', methods=['POST'])
def upload_file():
    # check if the post request has the file part

    # create a special subfolder for the files uploaded this time
Ejemplo n.º 9
0
def main(_):
  model = ImageModel(FLAGS.image_model_name, 
                     FLAGS.image_height, 
                     FLAGS.image_width,
                     FLAGS.image_checkpoint_file)
  run(model)
Ejemplo n.º 10
0
def run():
    name = 'coco-nopeep'
    epochs = 200
    subdir = name + "-" + time.strftime("%Y%m%d-%H%M%S")
    if not os.path.isdir(subdir):
        os.mkdir(subdir)

    bs = 200
    data_train = MSCoco(dataset='train', num=82611, bs=bs)
    data_valid = MSCoco(dataset='val', num=4989, bs=bs)

    train_stream = DataStream.default_stream(data_train,
                                             iteration_scheme=SequentialScheme(
                                                 data_train.num_examples, bs))
    valid_stream = DataStream.default_stream(data_valid,
                                             iteration_scheme=SequentialScheme(
                                                 data_valid.num_examples, bs))

    img_height, img_width = (32, 32)

    x = T.matrix('features')
    #x.tag.test_value = np.random.rand(bs, 3*32*32).astype('float32')
    y = T.lmatrix('captions')
    #y.tag.test_value = np.random.rand(bs, 57).astype(int)
    mask = T.lmatrix('mask')
    #mask.tag.test_value = np.ones((bs,57)).astype(int)

    K = 25323
    lang_N = 57
    N = 32
    read_size = 9
    write_size = 9
    m = 256
    gen_dim = 550
    infer_dim = 550
    z_dim = 275
    l = 512

    model = ImageModel(bs,
                       K,
                       lang_N,
                       N,
                       read_size,
                       write_size,
                       m,
                       gen_dim,
                       infer_dim,
                       z_dim,
                       l,
                       image_size=32 * 32,
                       channels=3,
                       cinit=0.0)
    model._inputs = [x, y, mask]

    kl, log_recons, log_likelihood, c = model.train(x, y, mask)
    kl.name = 'kl'
    log_recons.name = 'log_recons'
    log_likelihood.name = 'log_likelihood'
    c.name = 'c'

    model._outputs = [kl, log_recons, log_likelihood, c]

    params = model.params

    #from solvers.RMSProp import RMSProp as solver
    lr = theano.shared(np.asarray(0.001).astype(theano.config.floatX))
    #updates = solver(log_likelihood, params, lr=lr, clipnorm=10.0)#0.001)
    #lr = 0.001
    grads = T.grad(log_likelihood, params)
    his = []
    for p in params:
        pz = p.get_value() * 0
        his.append(theano.shared(pz))

    threshold = 10.0
    decay = 0.9
    updates = OrderedDict()

    for p, ph, g in zip(params, his, grads):
        l2_norm = T.sqrt(T.sqr(g).sum())
        m = T.switch(l2_norm < threshold, 1, threshold / l2_norm)
        grad = m * g

        ph_n = decay * ph + (1 - decay) * grad**2
        updates[ph] = ph_n
        updates[p] = p - (lr / T.sqrt(ph_n + 1e-6)) * grad

    model._updates = updates

    logger.info('Compiling sample function')
    model.build_sample_function(y, mask)
    logger.info('Compiled sample function')

    # ============= TRAIN =========
    plots = [['train_kl', 'valid_kl'],
             ['train_log_recons', 'valid_log_recons'],
             ['train_log_likelihood', 'valid_log_likelihood']]
    main_loop = MainLoop(
        model,
        train_stream,
        [
            FinishAfter(epochs),
            Track(variables=['kl', 'log_recons', 'log_likelihood'],
                  prefix='train'),
            #TrackBest(variables=['kl'], prefix='train'),
            DataStreamTrack(valid_stream,
                            ['kl', 'log_recons', 'log_likelihood'],
                            prefix='valid'),
            SampleSentences(subdir, bs, 32, 32),
            DropLearningRate(lr, 25, 0.0001),
            Plot(name, plots, 'http://nameless-wave-6526.herokuapp.com/'),
            SaveModel(subdir, name + '.model'),
            TimeProfile(),
            Printing()
        ])
    main_loop.run()
Ejemplo n.º 11
0
import matplotlib.pyplot as plt
import pandas as pandas
import thread
from pyspark import SparkContext
from pyspark.streaming import StreamingContext
from tweepy import API
from tweepy.streaming import json
import tweet
from image_model import ImageModel
from streamer import PyStreamer

api = API()

sc = SparkContext('local[*]', 'Spark Media Inception v3')
my_streamer = PyStreamer(languages=["en"], topics=["dog", "cat"])
image_model = ImageModel(sc=sc)
classified = []


def draw_bar_graph(blah, classified):
    while True:
        if len(classified) == 0:
            sleep(10)
            continue
        df = pandas.DataFrame(classified, columns=['Class', 'Count'])
        df = df.groupby("Class").sum()
        classified_tmp = [(x, y) for (x, y) in df.itertuples()]
        print(classified_tmp)

        # Just get the top 5
        classified_tmp = sorted(classified_tmp,
Ejemplo n.º 12
0
                                random_state=354).reset_index(drop=True)

            print(
                f"{len(train_df)} Training Samples.\n{len(val_df)} Validation Samples.\n"
            )

            best_file = f'{config.neural_backbone}_best_fold{fold}_dropout_{config.dropout}.pth'
            if config.regression:
                best_file = 'regression_' + best_file

            log_filename = best_file.replace('.pth', '.txt')

            model = ImageModel(model_name=config.neural_backbone,
                               device=device,
                               dropout=config.dropout,
                               neurons=0,
                               num_classes=config.num_classes,
                               extras_inputs=[],
                               base_model_pretrained_weights=None)
            model.load(directory=config.output_dir, filename=best_file)
            model = model.to(device)

            if fine_tune:
                best_file = 'finetune_' + best_file

            checkpoint_path = os.path.join(config.output_dir, best_file)
            if os.path.exists(checkpoint_path):
                print(
                    f"WARNING: TRAINED CHECKPOINT ALREADY EXISTS IN {checkpoint_path}. "
                    f"SKIPPING TRAINING FOR THIS MODEL/FOLD")
            else: