def inception():
    # load pre-trained model graph, don't add final layer
    model = inception_v3.InceptionV3(include_top=False, input_shape=(IMG_SIZE, IMG_SIZE,3), weights='imagenet')
    # add global pooling just like in InceptionV3
    new_output = keras.layers.GlobalAveragePooling2D()(model.output)
    # add new dense layer for our labels
    new_output = keras.layers.Dense(N_CLASSES, activation='softmax')(new_output)
    model = keras.engine.training.Model(model.inputs, new_output)
    return model
def load_model():
    N_CLASSES = 3
    # Start with an Inception V3 model, not including the final softmax layer.
    base_model = inception.InceptionV3(weights='imagenet')
    print 'Loaded Inception model'

    # Add on new fully connected layers for the output classes.
    x = Dense(32, activation='relu')(base_model.get_layer('flatten').output)
    x = Dropout(0.5)(x)
    predictions = Dense(N_CLASSES, activation='softmax', name='predictions')(x)
    model = Model(input=base_model.input, output=predictions)
    model.compile(loss='categorical_crossentropy',
                  optimizer='sgd',
                  metrics=['accuracy'])
    model.load_weights('inception_happy_neutral_surprise.h5')
    return model
def generate_model():
	# Start with an Inception V3 model, not including the final softmax layer.	
	base_model = inception.InceptionV3(weights='imagenet')
	print 'Loaded Inception model'


        for layer in base_model.layers:
                layer.trainable = True


	# Add on new fully connected layers for the output classes.
	x = Dense(32, activation='relu')(base_model.get_layer('flatten').output)
	x = Dropout(0.5)(x)
	predictions = Dense(N_CLASSES, activation='softmax', name='predictions')(x)

	model = Model(input=base_model.input, output=predictions)

	model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['categorical_accuracy'])


	# Show some debug output
	print (model.summary())

	return model
correct_skipgram_vector = np.array(correct_skipgram)
#Now we have the correct skipgram vectors
print("Done with correct Skipgram vector  and its shape is: ",
      correct_skipgram_vector.shape)
incorrect = []
misclassified_word = []
for item in predictions:
    incorrect.append(item)
    misclassified_word.append(predictions[item][0])
incorrect = incorrect[0:len(predictions)]
misclassified_word = misclassified_word[0:len(predictions)]
#We need to get the activations for the Correct words as well

#Get the activations for incorrect
model = inception_v3.InceptionV3(include_top=True, weights='imagenet')
layer = str(sys.argv[1])
print("The passed layer is: ", layer)
correct_cnn_vector = []
for i in range(len(paths)):
    if vocab[i] not in correct:
        continue
    vec = get_act_vector(paths[i], model, layer)[0]
    vec = vec.flatten()
    vec = vec.tolist()
    correct_cnn_vector.append(vec)

correct_cnn_vector = np.array(correct_cnn_vector)
print("Done with correct CNN vector activations and its shape is: ",
      correct_cnn_vector.shape)
예제 #5
0
import numpy as np
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential, Model
from keras.layers import Dense, Activation, Flatten, Dropout
from keras.callbacks import CSVLogger
import inception_v3 as inception
import os

N_CLASSES = 10
IMSIZE = (299, 299)

base_model = inception.InceptionV3(weights='imagenet')

for layer in base_model.layers:
    layer.trainable = False

x = Dense(32, activation='relu')(base_model.get_layer('flatten').output)
x = Dropout(0.5)(x)
predictions = Dense(N_CLASSES, activation='sigmoid', name='predictions')(x)

model = Model(input=base_model.input, output=predictions)

model.compile(loss='binary_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])
model.save('Inception.h5')

x_train = np.load('X_train.npy')
y_train = np.load('Y_train.npy')
x_val = np.load('X_val.npy')
예제 #6
0
from keras.layers import Dense, Activation, Flatten, Dropout
import inception_v3 as inception
from keras.utils import np_utils

N_CLASSES = 3
IMSIZE = (299, 299)

# TO DO:: Replace these with paths to YOUR data.
# Training directory containing separate directories for each class
train_dir = '.../train'

# Testing directory containing separate directories for each class
test_dir = '.../validation'

# Start with an Inception V3 model, not including the final softmax layer.
base_model = inception.InceptionV3(include_top=False, weights='imagenet')
print 'Loaded Inception model'

# Turn off training on base model layers
for layer in base_model.layers:
    layer.trainable = False

#test_datagen = ImageDataGenerator()
test_datagen = ImageDataGenerator(rescale=1. / 255)
test_generator = test_datagen.flow_from_directory(
    test_dir,  # this is the target directory
    target_size=
    IMSIZE,  # all images will be resized to 299x299 Inception V3 input
    batch_size=32,
    class_mode=None,
    shuffle=False)
args = vars(ap.parse_args())

image_filename = args["image"]
# need to set the input shape to (299x299) [rather than (224x224)]
# and use a different image processing function
input_shape = (299, 299)
preprocess = network.preprocess_input

# load our the network weights from disk (NOTE: if this is the
# first time you are running this script for a given network, the
# weights will need to be downloaded first -- depending on which
# network you are using, the weights can be 90-575MB, so be
# patient; the weights will be cached and subsequent runs of this
# script will be *much* faster)
print("[INFO] loading {}...".format("InceptionV3"))
model = network.InceptionV3(weights="imagenet")

# load the input image using the Keras helper utility while ensuring
# the image is resized to `inputShape`, the required input dimensions
# for the ImageNet pre-trained network
print("[INFO] loading and pre-processing image...")
image = load_img(image_filename, target_size=input_shape)
image = img_to_array(image)

# our input image is now represented as a NumPy array of shape
# (inputShape[0], inputShape[1], 3) however we need to expand the
# dimension by making the shape (1, inputShape[0], inputShape[1], 3)
# so we can pass it through thenetwork
image = np.expand_dims(image, axis=0)

# pre-process the image using the appropriate function based on the
예제 #8
0
        print('resizing detail from %s to %s' %
              (detail.shape, octave_base.shape))
        detail = imresize(detail, octave_base.shape[:2])

    x = preprocess_image(octave_base + detail)

    dream = Input(shape=(3, img_shape[0], img_shape[1]))

    if args.model == 'resnet50':
        model = resnet50.ResNet50(include_top=False, input_tensor=dream)
    elif args.model == 'vgg16':
        model = vgg16.VGG16(include_top=False, input_tensor=dream)
    elif args.model == 'vgg19':
        model = vgg19.VGG19(include_top=False, input_tensor=dream)
    elif args.model == 'inception_v3':
        model = inception_v3.InceptionV3(include_top=False, input_tensor=dream)
    else:
        raise 'unknown model ' + args.model
    print('Model loaded.')

    loss_and_grads = create_loss_function(dream, settings, model, img_shape)
    evaluator = Evaluator(loss_and_grads)

    # run scipy-based optimization (L-BFGS) over the pixels of the generated image
    # so as to minimize the loss
    for i in range(10):
        print('Start of iteration', i)
        start_time = time.time()

        # add a random jitter to the initial image. This will be reverted at decoding time
        random_jitter = (settings['jitter'] * 2) * (np.random.random(x.shape) -
예제 #9
0
def main():
    '''read the dataset'''
    print('Using original ds')
    # Read the hdf5 data
    landmarks_dir = '/home/sushant/landmarks_seperate/'
    dataset_name = sys.argv[1]
    dataset_fullfile = landmarks_dir + sys.argv[1] + '.h5'
    print('Using ', dataset_fullfile)
    f = h5py.File(dataset_fullfile, 'r')
    train_imgs = f['train_imgs'][:]
    train_imgs -= train_imgs.mean()
    train_pose_tx = f['train_pose_tx'][:]
    train_pose_rt = f['train_pose_rt'][:]
    test_imgs = f['test_imgs'][:]
    test_pose_tx = f['test_pose_tx'][:]
    test_pose_rt = f['test_pose_rt'][:]
    test_imgs -= train_imgs.mean()
    print(train_pose_tx.shape)
    print(train_pose_rt.shape)

    #dataset_dir = '/home/sushant/kitti_dataset/'
    #train_file_name = dataset_dir + 'train.h5'
    #test_file_name = dataset_dir + 'test.h5'
    ##Read the train
    #f = h5py.File(train_file_name)
    #train_imgs = f['train_imgs'][:]
    #train_rt = f['train_R'][:]
    #train_tx = f['train_T'][:]
    #f.close()
    ## Read the test
    #f = h5py.File(test_file_name)
    #test_imgs = f['test_imgs'][:]
    #test_rt = f['test_R'][:]
    #test_tx = f['test_T'][:]
    #f.close()
    ## Subtract the train mean from the train and test set
    #train_imgs -= train_imgs.mean()
    #test_imgs -= train_imgs.mean()
    ## Display the number of hte trai and test samples SANITY CHECK
    #print('Train set has: ', train_imgs.shape[0], 'number of samples ')
    #print('Test set has: ', test_imgs.shape[0], 'number of samples ')
    # Model
    model1 = inception_v3.InceptionV3(include_top=False,
                                      weights='imagenet',
                                      input_shape=(224, 224, 3))
    orig_op = model1.output
    y = Flatten()(orig_op)
    y = Dense(1024, use_bias=True, name='cls1_fc1_pose', activation='tanh')(y)
    y = Dropout(0.5)(y)
    tx_1 = Dense(3,
                 name='tx_1',
                 use_bias=True,
                 kernel_initializer=RandomNormal(mean=0.0, stddev=0.5),
                 kernel_regularizer=regularizers.l2(0.01))(y)
    rx_1 = Dense(4,
                 name='rx_1',
                 use_bias=True,
                 kernel_initializer=RandomNormal(mean=0.0, stddev=0.5),
                 kernel_regularizer=regularizers.l2(0.01))(y)
    model = Model(inputs=model1.inputs, outputs=[tx_1, rx_1])
    sgd = optimizers.SGD(lr=0.000001, momentum=0.9, decay=1e-6)
    model.compile(optimizer=sgd, loss='mse', loss_weights=[1.0, 500.0])
    device = 'cpu:0'
    #tb_log_dir_name = '/home_logs/'
    with tf.device(device):
        #model = gru_pose_net(img_rows, img_cols, img_channels)
        # Use TensorBoard to generate graphs of loss
        #tb = TensorBoard(log_dir=tb_log_dir_name, histogram_freq=1,
        #write_graph=True, write_images=True)
        model.fit(train_imgs, [train_pose_tx, train_pose_rt],
                  batch_size=batch_size,
                  epochs=num_epochs,
                  shuffle=False)
        model.save(model_name)
        p_tx_3, p_rx_3 = model.predict(test_imgs)
        results = np.zeros((test_imgs.shape[0], 2), dtype='float32')
        for i in range(0, test_imgs.shape[0]):
            q2 = p_rx_3[i, :] / np.linalg.norm(p_rx_3[i, :])
            q1 = test_pose_rt[i, :] / np.linalg.norm(test_pose_rt[i, :])
            d = abs(np.sum(np.multiply(q1, q2)))
            theta = 2 * np.arccos(d) * 180 / math.pi
            error_x = np.linalg.norm(test_pose_tx[i, :] - p_tx_3[i, :])
            results[i, :] = [error_x, theta]
            print('Iteration:  ', i, '  Error XYZ (m):  ', error_x,
                  '  Error Q (degrees):  ', theta)
        median_result = np.median(results, axis=0)
        print('Median error meters: ', median_result[0])
        print('Median error degrees: ', median_result[1])
        np.savetxt('results.txt', results, delimiter=' ')
        print('Success!')
        K.clear_session()

    #model.add(GRU(1024, kernel_initializer = 'glorot_normal', kernel_regularizer=regularizers.l2(0.01)))
    #y = model.add(Dropout(0.5))
    #model.add(Dense(3, name='tx_3', use_bias = True, kernel_initializer=
    #RandomNormal(mean=0.0, stddev=0.5), kernel_regularizer=regularizers.l2(0.01))
    print(model.summary())

    print('================')
예제 #10
0
import inception_v3


def save_proto(proto, prototxt):
    with open(prototxt, 'w') as f:
        f.write(str(proto))


if __name__ == '__main__':
    model = inception_v3.InceptionV3('imagenet_test_lmdb', 'imagenet_train_lmdb', 1000)

    train_proto = model.inception_v3_proto(64)
    test_proto = model.inception_v3_proto(64, phase='TEST')

    save_proto(train_proto, 'imagenet_train.prototxt')
    save_proto(test_proto, 'imagenet_test.prototxt')