Exemple #1
0
 def build_theano_fn_resnet(self):
     t0 = time.time()
     print '%s build theano fn resnet' % self.rank
     x = T.ftensor4('images')
     y = T.ivector('label')
     model = resnet50.build_model(x)
     prob = lasagne.layers.get_output(model['prob'], deterministic=True)
     self.params = lasagne.layers.get_all_params(model['prob'],
                                                 trainable=True)
     cost = -T.log(prob[T.arange(prob.shape[0]), y] + 1e-6).mean()
     grads = T.grad(cost, self.params)
     grads_all_reduced = self.grad_all_reduce(grads)
     updates = adam(grads_all_reduced, self.params)
     self.train_fn = theano.function([x, y], [cost, y],
                                     updates=updates,
                                     accept_inplace=True)
     print '%s finished build theano fn, used %.3f' % (self.rank,
                                                       time.time() - t0)
Exemple #2
0
    formatter = logging.Formatter('%(asctime)s %(message)s', '%m/%d/%Y %I:%M:%S %p')
    fh = logging.FileHandler('ln_hs_mscoco_captions.log')
    fh.setLevel(logging.DEBUG)
    fh.setFormatter(formatter)

    sh = logging.StreamHandler(sys.stdout)
    sh.setLevel(logging.INFO)
    sh.setFormatter(formatter)

    logger.addHandler(fh)
    logger.addHandler(sh)

    logger.info('Loading the ResNet50 model.')
    # First get the ResNet50 model
    resnet_weights_path = '/home/noury/modelzoo/resnet50.pkl'
    resnet = build_model()
    model_params = pickle.load(open(resnet_weights_path, 'rb'))
    lasagne.layers.set_all_param_values(resnet['prob'], model_params['values'])
    mean_im = model_params['mean_image'].reshape((1, 3, 224, 224)).astype(np.float32)

    # Load the files needed for the MS COCO Captions dataset
    train_images_path = '/home/noury/datasets/mscoco/train2014'
    valid_images_path = '/home/noury/datasets/mscoco/val2014'
    train_annotations_filepath = '/home/noury/datasets/mscoco/annotations/captions_train2014.json'
    valid_annotations_filepath = '/home/noury/datasets/mscoco/annotations/captions_val2014.json'
    coco_captions = pickle.load(open('coco_captions_trainval2014.pkl', 'rb'))
    train_buckets = coco_captions['train buckets']
    valid_buckets = coco_captions['valid buckets']
    wordset = coco_captions['raw wordset']
    word2idx = {}
    word2idx['<PAD>'] = 0
Exemple #3
0
num_epochs = 20
hidden_layer = 512

model_name = 'RESNET50'  # 'VGG19','RESNET50'

X_train, X_valid, Y_train, Y_valid = utils.split_train_valid(
    utils.read_all_training_file(
        train_folder='./input/train/', label_file='./input/labels.csv', image_size=img_size),
    test_percentage=0.2)
num_of_clases = 120 #Y_train.shape[1]

if model_name == 'VGG19':
    model = vgg19.build_model(
        num_of_clases, image_size=img_size, hidden_layer=hidden_layer)
elif model_name == 'RESNET50':
    model = resnet50.build_model(
        num_of_clases, image_size=img_size, hidden_layer=hidden_layer)

model.compile(loss='categorical_crossentropy',
              optimizer='adam', metrics=['accuracy'])
model.summary()
# model.fit(x=X_train, y=Y_train, batch_size=batch,
#          epochs=num_epochs, validation_data=(X_valid, Y_valid))

# data augmentation
generator = utils.augment_image_generator()
train_generator = generator.flow(
    x=X_train, y=Y_train, batch_size=batch, shuffle=True)

#fit model
model.fit_generator(generator=train_generator,
                    steps_per_epoch=X_train.shape[0] // batch,