Exemple #1
0
class TLClassifier:
    def __init__(self, is_site):
        #TODO load classifier
        assert not is_site
        weights_file = r'light_classification/models/squeezenet_weights.h5'  #Replace with real world classifier

        image_shape = (224, 224, 3)

        self.states = (TrafficLight.RED, TrafficLight.YELLOW,
                       TrafficLight.GREEN, TrafficLight.UNKNOWN)

        print('Loading model..')
        self.model = SqueezeNet(len(self.states), *image_shape)
        self.model.load_weights(weights_file, by_name=True)
        self.model._make_predict_function()
        print('Loaded weights: %s' % weights_file)

    def get_classification(self, image):
        """Determines the color of the traffic light in the image

        Args:
            image (cv::Mat): image containing the traffic light

        Returns:
            int: ID of traffic light color (specified in styx_msgs/TrafficLight)

        """
        mini_batch = cv2.resize(
            image, (224, 224),
            cv2.INTER_AREA).astype('float')[np.newaxis, ..., ::-1] / 255.
        light = self.states[np.argmax(self.model.predict(mini_batch))]

        return light
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--checkpoint-path', required=True)
    parser.add_argument('--image', nargs='+', required=True)
    parser.add_argument('--num-classes', type=int, required=True)
    args = parser.parse_args()

    model = SqueezeNet(weights=None, classes=args.num_classes)
    model.load_weights(args.checkpoint_path)

    xs = []
    for path in args.image:
        img = image.load_img(path, target_size=(SIZE, SIZE))
        x = image.img_to_array(img)
        xs.append(x)

    xs = np.array(xs)
    xs = preprocess_input(xs)

    probs = model.predict(xs)

    print('')
    for i, path in enumerate(args.image):
        print('%s' % path)
        print('    Prediction: %s' % np.argmax(probs[i]))
def predict(img_local_path):
    model = SqueezeNet(weights='imagenet')
    img = image.load_img(img_local_path, target_size=(227, 227))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    preds = model.predict(x)
    result = decode_predictions(preds)
    return result
Exemple #4
0
 def build_model(self):
     self.model = SqueezeNet(include_top=True,
                             weights=None,
                             classes=3,
                             input_shape=(self.IMAGE_SIZE, self.IMAGE_SIZE,
                                          3))
     self.model.summary()
     opt = Adam()
     self.model.compile(loss="binary_crossentropy",
                        optimizer=opt,
                        metrics=["accuracy"])
     return self.model
Exemple #5
0
def create_discriminator():
    """
    Makes the discriminator network
    :return: The discriminator network
    """
    # We are using the SqueezeNet because it is fairly resource-light and GANs can already be hard to train
    disc_net = SqueezeNet(input_width=input_size[0], classes=2)

    # Compile with loss
    disc_net.compile(loss="binary_crossentropy",
                     optimizer=create_adam(),
                     metrics=["accuracy"])

    return disc_net
Exemple #6
0
    def __init__(self, is_site):
        #TODO load classifier
        assert not is_site
        weights_file = r'light_classification/models/squeezenet_weights.h5'  #Replace with real world classifier

        image_shape = (224, 224, 3)

        self.states = (TrafficLight.RED, TrafficLight.YELLOW,
                       TrafficLight.GREEN, TrafficLight.UNKNOWN)

        print('Loading model..')
        self.model = SqueezeNet(len(self.states), *image_shape)
        self.model.load_weights(weights_file, by_name=True)
        self.model._make_predict_function()
        print('Loaded weights: %s' % weights_file)
def get_pretrained_model(model_name,
                         input_shape,
                         include_top=False,
                         weights="imagenet",
                         pooling=None):
    if model_name == "vgg16":
        return VGG16(include_top=include_top,
                     weights=weights,
                     input_shape=input_shape,
                     pooling=pooling)
    if model_name == "vgg19":
        return VGG19(include_top=include_top,
                     weights=weights,
                     input_shape=input_shape,
                     pooling=pooling)
    if model_name == "resnet50":
        return ResNet50(include_top=include_top,
                        weights=weights,
                        input_shape=input_shape,
                        pooling=pooling)
    if model_name == "inceptionV3":
        return InceptionV3(include_top=include_top,
                           weights=weights,
                           input_shape=input_shape,
                           pooling=pooling)
    if model_name == "xception":
        return Xception(include_top=include_top,
                        weights=weights,
                        input_shape=input_shape,
                        pooling=pooling)
    if model_name == "squeezenet":
        return SqueezeNet(include_top=include_top,
                          weights=weights,
                          input_shape=input_shape)
Exemple #8
0
def PSPNet(classnum=81, input_shape=(473, 473), sq=False):
    inp = Input(input_shape + (3,), name="psp_input")
    if sq:
        res = SqueezeNet(input_tensor=inp, include_top=False, input_shape=(800,800) + (3,))
        x = Lambda(Interp, arguments={'shape': (60, 60)})(res.output)
    else:
        x = ResNet(inp, layers=50)
        x = Lambda(Interp, arguments={'shape': (60, 60)})(x)
    psp = build_psp(x, input_shape)

    x = Conv2D(512, (3, 3), strides=(1, 1), padding="same", name="conv5_4", use_bias=False)(psp)
    x = BN(name="conv5_4_bn")(x)
    embedFeatures = Activation('relu')(x)
    x = Dropout(0.1)(embedFeatures)

    classFeatures = Conv2D(classnum, (1, 1), strides=(1, 1), name="lastConv")(x)
    x = Lambda(Interp, arguments={'shape': (input_shape[0], input_shape[1])})(classFeatures)
    classes = Activation('sigmoid')(x)

    model = Model(inputs=inp, outputs=classes)
    if not sq:
        model.load_weights('/mnt/course/weights/pretrained.h5', by_name=True)
    # for layer in model.layers[:-8]:
    #     layer.trainable = False
    sgd = SGD(lr=0.01, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd,
                  loss='mse',
                  metrics=['accuracy'])
    return model
def get_squeezenet(input_shape, n_classes, **params):
    """
    """
    optimizer = '' if 'optimizer' not in params else params['optimizer']
    lr = 0.01 if 'lr' not in params else params['lr']
    loss = '' if 'loss' not in params else params['loss']
    weights = 'imagenet' if 'weights' not in params else params['weights']
    final_activation = 'sigmoid'

    snet = SqueezeNet(input_shape=input_shape,
                      classes=n_classes,
                      include_top=False,
                      weights=weights)

    x = snet.outputs[0]
    x = GlobalAveragePooling2D()(x)

    out = Activation(final_activation, name='tag_vector')(x)
    model = Model(inputs=snet.inputs, outputs=out)

    model.name = "SqueezeNet"

    if optimizer == 'adadelta':
        opt = Adadelta(lr=lr)
    elif optimizer == 'adam':
        opt = Adam(lr=lr)
    elif optimizer == 'sgd':
        opt = SGD(lr=lr, momentum=0.9, decay=0.00001, nesterov=True)
    else:
        opt = None

    if opt is not None:
        model.compile(loss=loss, optimizer=opt, metrics=[precision, recall])
    return model
Exemple #10
0
def create_model(input_shape, n_out):
    input_tensor = Input(shape=(SIZE, SIZE, 3))
    #bn = BatchNormalization()(input_tensor)
    #conv = Conv2D(3,(3,3),padding='same',activation='relu')(bn)
    base_model = SqueezeNet(include_top=False,
                            weights='imagenet',
                            input_shape=(SIZE, SIZE, 3),
                            input_tensor=input_tensor)

    enc_out, short_cuts = encoder(base_model)
    x0 = GlobalAveragePooling2D()(squeeze_excite_block(enc_out))
    x1 = GlobalAveragePooling2D()(squeeze_excite_block(short_cuts[0]))
    x2 = GlobalAveragePooling2D()(squeeze_excite_block(short_cuts[1]))
    x3 = GlobalAveragePooling2D()(squeeze_excite_block(short_cuts[2]))
    #x4 = GlobalAveragePooling2D()(squeeze_excite_block(short_cuts[3]))
    x = Concatenate()([x0, x1, x2, x3])
    x = BatchNormalization()(x)
    x = Dropout(0.5)(x)
    x = Dense(256, activation='relu')(x)
    #x = BatchNormalization()(x)
    #x = Dropout(0.5)(x)
    #x = Dense(256, activation='relu')(x)
    x = BatchNormalization()(x)
    x = Dropout(0.5)(x)
    output = Dense(n_out, activation='sigmoid')(x)

    model = Model(input_tensor, output)

    # transfer imagenet weights
    #res_img = ResNet34(include_top=False, weights='imagenet', input_shape=(SIZE, SIZE, 3))
    #offset = 2
    #for i, l in enumerate(base_model.layers[offset+1:]):
    #    l.set_weights(res_img.layers[i + 1].get_weights())

    return model
Exemple #11
0
def get_squeezenet_model(layers):

    base_model = SqueezeNet(content_layers)

    all_n = []
    
    all_n = get_ckpt_weights('../squeezenet_weights/squeezenet.ckpt')
    all_weights = []
    for w in base_model.model.weights:
        all_weights.append(all_n[weight_to_weight[w.name]])
    base_model.set_weights(all_weights)
    base_model.trainable = False

    dream_model = base_model 

    return dream_model
def get_squeezenet21(input_shape, n_classes, **params):
    """
    """
    optimizer = '' if 'optimizer' not in params else params['optimizer']
    lr = 0.01 if 'lr' not in params else params['lr']
    loss = '' if 'loss' not in params else params['loss']
    weights = 'imagenet' if 'weights' not in params else params['weights']

    snet = SqueezeNet(input_shape=input_shape,
                      classes=n_classes,
                      include_top=False,
                      weights=weights)

    x = snet.outputs[0]
    x = Flatten()(x)
    x = Dense(96, activation='relu', name='d1')(x)
    x = Dropout(0.5)(x)
    x = Dense(n_classes, name='d2')(x)
    out = Activation('sigmoid', name='tags')(x)
    model = Model(inputs=snet.inputs, outputs=out)

    model.name = "SqueezeNet21"

    if optimizer == 'adadelta':
        opt = Adadelta(lr=lr)
    elif optimizer == 'adam':
        opt = Adam(lr=lr)
    elif optimizer == 'sgd':
        opt = SGD(lr=lr, momentum=0.9, decay=0.00001, nesterov=True)
    else:
        opt = None

    if opt is not None:
        model.compile(loss=loss, optimizer=opt, metrics=[precision, recall])
    return model
Exemple #13
0
def create_squeezenet(nclass, train=True):
    input = Input(shape=(HEIGHT, WIDTH, 3), name="img_input")
    base_model = SqueezeNet(include_top=False,
                            weights=None,
                            input_tensor=input)
    x = base_model.output

    if train:
        base_model.load_weights(
            "models/squeezenet_weights_tf_dim_ordering_tf_kernels_notop.h5")
        x = Dropout(0.2, name='drop9')(x)
    x = Convolution2D(nclass, (1, 1), padding='valid', name='conv10')(x)
    x = Activation('relu', name='relu_conv10')(x)
    x = GlobalAveragePooling2D()(x)
    x = Activation('softmax', name='loss')(x)
    model = Model(input, x, name='squeezenet')
    return model
 def __init__(self, session):
     self.session = session
     self.add_placeholders()
     self.squeezenet = SqueezeNet(save_path='squeezenet/squeezenet.ckpt',
                                  sess=session)
     self.y_out = self.add_prediction_op()
     self.loss = self.add_loss_op(self.y_out)
     self.training_op = self.add_optimization_op(self.loss)
     self.correct_prediction, self.accuracy = self.add_accuracy_op()
Exemple #15
0
def main():
    test_patterns = [
        ('VGGNetBN', VGGNetBN(17), 224), ('VGGNetBNHalf', VGGNetBN(17,
                                                                   32), 224),
        ('VGGNetBNQuater', VGGNetBN(17, 16), 224),
        ('GoogLeNetBN', GoogLeNetBN(17), 224),
        ('GoogLeNetBNHalf', GoogLeNetBN(17, 16), 224),
        ('GoogLeNetBNQuater', GoogLeNetBN(17, 8), 224),
        ('ResNet50', ResNet50(17), 224), ('ResNet50Half', ResNet50(17,
                                                                   32), 224),
        ('ResNet50Quater', ResNet50(17, 16), 224),
        ('SqueezeNet', SqueezeNet(17), 224),
        ('SqueezeNetHalf', SqueezeNet(17, 8), 224),
        ('MobileNet', MobileNet(17), 224),
        ('MobileNetHalf', MobileNet(17, 16), 224),
        ('MobileNetQuater', MobileNet(17, 8), 224),
        ('InceptionV4', InceptionV4(dim_out=17), 299),
        ('InceptionV4S',
         InceptionV4(dim_out=17,
                     base_filter_num=6,
                     ablocks=2,
                     bblocks=1,
                     cblocks=1), 299),
        ('InceptionResNetV2', InceptionResNetV2(dim_out=17), 299),
        ('InceptionResNetV2S',
         InceptionResNetV2(dim_out=17,
                           base_filter_num=8,
                           ablocks=1,
                           bblocks=2,
                           cblocks=1), 299),
        ('FaceClassifier100x100V', FaceClassifier100x100V(17), 100),
        ('FaceClassifier100x100V2', FaceClassifier100x100V2(17), 100)
    ]

    for model_name, model, test_size in test_patterns:
        oltp_cpu, batch_gpu = check_speed(model, test_images[test_size])
        print('{}\t{:.02f}\t{:.02f}'.format(model_name, oltp_cpu * 1000,
                                            batch_gpu * 1000))
Exemple #16
0
def train(img_local_path, label_path, model_object_key):
    model = SqueezeNet(weights='imagenet')
    img = image.load_img(img_local_path, target_size=(227, 227))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    label_file = open(label_path)
    y = np.array([label_file.read()])
    label_file.close()

    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])

    history = model.fit(x, y)
    model.summary()
    model.save_weights(tmp_path + model_object_key)
    return history.history
def main():
    pl_train, pl_labels = get_dataset('./Pan_Licence/')
    pl_labels = to_categorical(pl_labels, num_classes=36)

    x_train, x_val, y_train, y_val = train_test_split(pl_train,
                                                      pl_labels,
                                                      test_size=0.2,
                                                      random_state=2064)

    tb = TensorBoard(log_dir='./logs/Squeezenet', write_graph=True)

    model = SqueezeNet()
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    print(model.summary())

    history = model.fit(x_train,
                        y_train,
                        batch_size=32,
                        epochs=5,
                        validation_split=0.1,
                        shuffle=True,
                        callbacks=[tb])

    ## Save Model
    json_model = model.to_json()
    with open('model_squeezenet.json', 'w') as f:
        f.write(json_model)
    model.save_weights('model_squeezenet.h5')
    print('Model Saved')

    print('Evaluating Model')
    predict = model.evaluate(x=x_val, y=y_val, batch_size=1)

    print('Score', predict[1] * 100.00)
    print('Loss', predict[0])
Exemple #18
0
def init():

    global model

    model_root = Model.get_model_path(
        'kd_teach_the_student')  #, _workspace=ws)
    # model_root = model_root.strip('model.json')
    print(model_root)
    # load json and create model

    weight_file = os.path.join(model_root, "squeezenet_weights.hdf5")

    model = SqueezeNet(weight_decay=0.0,
                       image_size=299,
                       trainable=False,
                       weight_file=weight_file)
    # model.load_weights(os.path.join(model_root, "squeezenet_weights.hdf5"))

    model = Model_keras(model.input, model.outputs)
def get_model(learning_rate=1e-3, num_classes=200):

    model = SqueezeNet(num_classes=num_classes)

    # set the first layer not trainable
    model.features[0].weight.requires_grad = False

    # all conv layers except the first and the last
    all_conv_weights = [
        (n, p) for n, p in model.named_parameters()
        if 'weight' in n and not 'bn' in n and not 'features.1.' in n
    ]
    weights_to_be_quantized = [
        p for n, p in all_conv_weights
        if not ('classifier' in n or 'features.0.' in n)
    ]

    # the last layer
    weights = [model.classifier[1].weight]
    biases = [model.classifier[1].bias]

    # parameters of batch_norm layers
    bn_weights = [
        p for n, p in model.named_parameters()
        if ('bn' in n or 'features.1.' in n) and 'weight' in n
    ]
    bn_biases = [
        p for n, p in model.named_parameters()
        if ('bn' in n or 'features.1.' in n) and 'bias' in n
    ]

    params = [{
        'params': weights,
        'weight_decay': 1e-4
    }, {
        'params': weights_to_be_quantized
    }, {
        'params': biases
    }, {
        'params': bn_weights
    }, {
        'params': bn_biases
    }]
    optimizer = optim.Adam(params, lr=learning_rate)

    # loss function
    criterion = nn.CrossEntropyLoss().cuda()
    # move the model to gpu
    model = model.cuda()
    return model, criterion, optimizer
Exemple #20
0
def train_chesspiece_model():
    """Trains the chesspiece model based on SqueezeNet-v1.1."""
    base_model = SqueezeNet(input_shape=(227, 227, 3), include_top=False,
                            weights='imagenet')

    # First train only the top layers
    for layer in base_model.layers:
        layer.trainable = False

    model = build_model(base_model)

    train_generator, validation_generator = data_generators(
        preprocess_input, (227, 227), 64)

    callbacks = model_callbacks(5, "./models/SqueezeNet1p1_pre.h5", 0.1, 10)

    history = train_model(model, 20, train_generator, validation_generator,
                          callbacks, use_weights=False, workers=5)

    plot_model_history(history, "./models/SqueezeNet1p1_pre_acc.png",
                       "./models/SqueezeNet1p1_pre_loss.png")
    evaluate_model(model, validation_generator)

    # Also train fire 7-9
    for layer in model.layers[:41]:
        layer.trainable = False
    for layer in model.layers[41:]:
        layer.trainable = True

    model.compile(optimizer='Adam', loss='categorical_crossentropy',
                  metrics=['accuracy'])

    callbacks = model_callbacks(20, "./models/SqueezeNet1p1.h5", 0.2, 8)

    history = train_model(model, 100, train_generator, validation_generator,
                          callbacks, use_weights=False, workers=5)

    plot_model_history(history, "./models/SqueezeNet1p1_acc.png",
                       "./models/SqueezeNet1p1_loss.png")
    evaluate_model(model, validation_generator)

    model.save("./models/SqueezeNet1p1_last.h5")
Exemple #21
0
def get_model():

    model = SqueezeNet()

    # create different parameter groups
    weights = [
        p for n, p in model.named_parameters()
        if 'weight' in n and not 'bn' in n and not 'features.1.' in n
    ]
    biases = [model.classifier[1].bias]
    bn_weights = [
        p for n, p in model.named_parameters()
        if ('bn' in n or 'features.1.' in n) and 'weight' in n
    ]
    bn_biases = [
        p for n, p in model.named_parameters()
        if ('bn' in n or 'features.1.' in n) and 'bias' in n
    ]

    # initialize batch norm params
    for p in bn_weights:
        constant(p, 1.0)
    for p in bn_biases:
        constant(p, 0.0)

    params = [{
        'params': weights,
        'weight_decay': 1e-3
    }, {
        'params': biases
    }, {
        'params': bn_weights
    }, {
        'params': bn_biases
    }]
    optimizer = optim.SGD(params, lr=4e-2, momentum=0.95, nesterov=True)

    # loss function
    criterion = nn.CrossEntropyLoss().cuda()
    # move the model to gpu
    model = model.cuda()
    return model, criterion, optimizer
def get_model():

    model = SqueezeNet()

    # create different parameter groups
    weights = [
        p for n, p in model.named_parameters()
        if 'weight' in n and 'bn' not in n and 'features.1.' not in n
    ]
    biases = [model.classifier[1].bias]
    bn_weights = [
        p for n, p in model.named_parameters()
        if ('bn' in n or 'features.1.' in n) and 'weight' in n
    ]
    bn_biases = [
        p for n, p in model.named_parameters()
        if ('bn' in n or 'features.1.' in n) and 'bias' in n
    ]

    for p in bn_weights:
        constant(p, 1.0)
    for p in bn_biases:
        constant(p, 0.0)

    params = [{
        'params': weights,
        'weight_decay': 3e-4
    }, {
        'params': biases
    }, {
        'params': bn_weights
    }, {
        'params': bn_biases
    }]
    optimizer = optim.SGD(params, lr=4e-2, momentum=0.95, nesterov=True)

    loss = nn.CrossEntropyLoss().cuda()
    model = model.cuda()  # move the model to gpu
    return model, loss, optimizer
Exemple #23
0
     model = DenseNet121()
 elif args.model == 'densenet121cov':
     MODEL_NAME = 'DenseNet121Cov'
     model = DenseNet121Cov()
 elif args.model == 'densenet121covdropout':
     MODEL_NAME = 'DenseNet121CovDropout'
     model = DenseNet121CovDropout()
 elif args.model == 'stndensenet121covdropout':
     MODEL_NAME = 'STNDenseNet121CovDropout'
     model = STNDenseNet121CovDropout()
 elif args.model == 'inceptionresnetv2':
     MODEL_NAME = 'InceptionResNetV2'
     model = InceptionResNetV2()
 elif args.model == 'squeezenet':
     MODEL_NAME = 'SqueezeNet'
     model = SqueezeNet()
 elif args.model == 'attentionresnet56':
     MODEL_NAME = 'AttentionResNet56'
     model = AttentionResNet56(shape=(192, 192, 3),
                               n_classes=8,
                               n_channels=32)
 elif args.model == 'lightcnn':
     MODEL_NAME = 'LightCNN'
     model = LightCNN()
 elif args.model == 'lightcnncov':
     MODEL_NAME = 'LightCNNCov'
     model = LightCNNCov()
 elif args.model == 'holonet':
     MODEL_NAME = 'HoloNet'
     model = HoloNet()
 elif args.model == 'holonetcov':
Exemple #24
0
    cell_fw = tf.contrib.rnn.LSTMCell(
        hps['num_hidden'],
        initializer=tf.random_uniform_initializer(-0.1, 0.1, seed=123),
        state_is_tuple=True)

    (emb_encoder_outputs,
     fw_state) = tf.contrib.rnn.static_rnn(cell_fw,
                                           emb_encoder_inputs,
                                           dtype=tf.float32,
                                           sequence_length=question_lens)
    return (emb_encoder_outputs, fw_state)


#load CNN
sess = tf.InteractiveSession()
CNN = SqueezeNet(save_path=PATH_TO_SAVED_NET, sess=sess)

#load question answer data
val_data = load_data()

print("=========== Data loaded ===========")

#process loaded data
# words = [set(sample['question'].split(" ")) for sample in val_data]
# set_words = set()
# for s in words:
#     set_words = set_words.union(s)

# hps['vsize'] = set_words.__len__()
words_to_idx = wordsToIdx()
idx_to_words = idxToWords()
import numpy as np
from keras.applications.imagenet_utils import preprocess_input, decode_predictions
from keras.preprocessing import image
from squeezenet import SqueezeNet
#import matplotlib.pyplot as plt
#import matplotlib.image as mpimg


model = SqueezeNet()

img = image.load_img('pexels-photo-280207.jpeg', target_size=(227, 227))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)
all_results = decode_predictions(preds)
for results in all_results:
    for result in results:
        print('Probability %0.2f%% => [%s]' % (100*result[2], result[1]))
        #result_text= 'Probability %0.2f%% => [%s]' % (100*result[2], result[1])
        #break
#plt.figure(num=1,figsize=(8, 6), dpi=80)
#plt.imshow(img)
#plt.text(130,90,result_text,horizontalalignment='center', verticalalignment='center',fontsize=16,color='black')
#plt.axis('off')
#plt.show()

Exemple #26
0
training_data = gen(X_train, Y_train, nb=200)
validation_data = gen(X_test, Y_test, nb=50)

# Unzip to two lists.
# images, classes = zip(*images_classes)
# [images, classes], [x, y] = mnist.load_data()
# images = images[0:500]
# classes = classes[0:500]

# images = np.array([cv2.resize(cv2.cvtColor(im, cv2.COLOR_GRAY2RGB), (227, 227)) for im in images])
# images = np.array(images)
# print images.shape
# classes = to_categorical(classes, nb_classes=nr_classes)

print('Loading model..')
model = SqueezeNet(nb_classes, input_shape=input_shape)
adam = Adam(lr=0.040)
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss="categorical_crossentropy",
              optimizer='adam',
              metrics=['accuracy'])
if os.path.isfile(weights_file):
    print('Loading weights: %s' % weights_file)
    model.load_weights(weights_file, by_name=True)

print('Fitting model')
# model.fit(images, classes, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_split=0.2, initial_epoch=0)
model.fit_generator(training_data,
                    samples_per_epoch=samples_per_epoch,
                    validation_data=validation_data,
                    nb_val_samples=nb_val_samples,
Exemple #27
0
def train_factory(MODEL_NAME):

    config = tf.ConfigProto()
    config.gpu_options.allocator_type = 'BFC'
    config.gpu_options.allow_growth = True
    set_session(tf.Session(config=config)) 
    # model = CCR(input_shape=(img_width,img_height,1),classes=charset_size)
    # model = LeNet.build(width=img_width, height=img_height, depth=1, classes=charset_size)
    # model = ResNet.build_model(SHAPE=(img_width,img_height,1), classes=charset_size)

    # vgg net 5
    # MODEL_PATH='trained_model/vggnet5.hdf5'
    # model=VGGNet5.vgg(input_shape=(img_width,img_height,1),classes=charset_size)

    model=None
    if(MODEL_NAME=='inception_resnet_v2'):
        model=InceptionResNetV2.inception_resnet_v2(input_shape=(img_width,img_height,3),classes=charset_size,weights='./trained_model/inception_resnet_v2/inception_resnet_v2.12-0.8244.hdf5')
    elif(MODEL_NAME=='xception'):
        # xeception
        model=Xception.Xception((img_width,img_height,3),classes=charset_size)
    elif(MODEL_NAME=='mobilenet_v2'):
        #mobilenet v2
        model=MobileNetv2.MobileNet_v2((img_width,img_height,3),classes=charset_size)
    elif(MODEL_NAME=='inception_v3'):
        #mobilenet v2
        model=Inception_v3.inception((img_width,img_height,3),classes=charset_size)
    elif(MODEL_NAME=='vgg16'):
        model=VGGNet.vgg(input_shape=(img_width,img_height,3),classes=charset_size)
    elif(MODEL_NAME=='vgg19'):
        model=VGG19.VGG19(input_shape=(img_width,img_height,3),classes=charset_size,weights='weights/vgg19_weights_tf_dim_ordering_tf_kernels.h5')
    elif(MODEL_NAME=='resnet50'):
        model=ResNet50.resnet(input_shape=(img_width,img_height,3),classes=charset_size)
    elif(MODEL_NAME=='inception_v4'):
        model=inception_v4.inception_v4(input_shape=(img_width,img_height,3),classes=charset_size)
    elif(MODEL_NAME=='resnet34'):
        model=ResNet34.ResNet34(input_shape=(img_width,img_height,3),classes=charset_size)
    elif(MODEL_NAME=='densenet121'):
        model=DenseNet.DenseNet(input_shape=(img_width,img_height,3),classes=charset_size)
    elif(MODEL_NAME=='densenet161'):
        model=DenseNet.DenseNet(input_shape=(img_width,img_height,3),classes=charset_size)
    elif(MODEL_NAME=='shufflenet_v2'):
        model=ShuffleNetV2.ShuffleNetV2(input_shape=(img_width,img_height,3),classes=charset_size)
    elif(MODEL_NAME=='resnet_attention_56'):
        model=Resnet_Attention_56.Resnet_Attention_56(input_shape=(img_width,img_height,3),classes=charset_size)
    elif(MODEL_NAME=='squeezenet'):
        model=SqueezeNet.SqueezeNet(input_shape=(img_width,img_height,3),classes=charset_size)
    elif(MODEL_NAME=='seresnet50'):
        model=SEResNet50.SEResNet50(input_shape=(img_width,img_height,3),classes=charset_size)
    elif(MODEL_NAME=='se_resnext'):
        model=SEResNext.SEResNext(input_shape=(img_width,img_height,3),classes=charset_size)
    elif(MODEL_NAME=='nasnet'):
        model=NASNet.NASNetLarge(input_shape=(img_width,img_height,3),classes=charset_size)
    elif(MODEL_NAME=='custom'):
        model=Custom_Network.Custom_Network(input_shape=(img_width,img_height,3),classes=charset_size)
    elif(MODEL_NAME=='resnet18'):
        model=ResnetBuilder.build_resnet_18(input_shape=(img_width,img_height,3),num_outputs=charset_size)



    print(model.summary())
    train(model,MODEL_NAME)
Exemple #28
0
def model_fn(features, labels, mode, params):
    assert params['n_classes'] > 0

    logits = SqueezeNet(features,
                        classes=params['n_classes'],
                        training=(mode == tf.estimator.ModeKeys.TRAIN))

    onehot_labels = tf.one_hot(labels, params['n_classes'], axis=1)

    # Compute loss.
    # See https://stats.stackexchange.com/questions/306862/cross-entropy-versus-mean-of-cross-entropy
    loss = tf.reduce_mean(tf.losses.softmax_cross_entropy(onehot_labels=onehot_labels,
                                                          logits=logits))

    # Compute predictions
    predictions = tf.cast(tf.argmax(logits, 1), tf.int32)
    labels = tf.cast(labels, tf.int64)

    predictions = tf.Print(predictions, [predictions], message='predictions', summarize=10000)

    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode, predictions={
            "classes": tf.argmax(input=logits, axis=1),
            "probabilities": tf.nn.softmax(logits, name="softmax_tensor")
        })

    # NOTE: Useful for debugging!
    loss = tf.Print(loss, [loss, tf.argmax(tf.nn.softmax(logits), axis=1)],
                    message='[Loss|Logits]',
                    summarize=1 + params['batch_size'] * 3)

    # Compute evaluation metrics.
    accuracy = tf.metrics.accuracy(labels, predictions, name='accuracy_1')

    metrics = {
        "accuracy": accuracy,
        "recall_at_5": tf.metrics.recall_at_k(labels, logits, 5),
        "recall_at_1": tf.metrics.recall_at_k(labels, logits, 1)
    }

    tf.summary.scalar('accuracy', accuracy[1])

    if mode == tf.estimator.ModeKeys.EVAL:
        return tf.estimator.EstimatorSpec(mode,
                                          loss=loss,
                                          eval_metric_ops=metrics)

    # Create training op.
    assert mode == tf.estimator.ModeKeys.TRAIN

    optimizer = None
    if params['optimizer'] == 'poly':
        # NOTE: Setting the learning rate to 0.04 gave `NanLossDuringTrainingError` as per the paper.
        decay_steps = params['n_images'] * params['n_epochs'] // params['batch_size']
        learning_rate = tf.train.polynomial_decay(learning_rate=params['lr'],
                                                  global_step=tf.train.get_global_step(),
                                                  decay_steps=decay_steps,
                                                  end_learning_rate=0.0005,
                                                  power=1.0,
                                                  cycle=False)

        optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate,
                                               momentum=0.9,
                                               use_nesterov=False)
    elif params['optimizer'] == 'rms':
        optimizer = tf.train.RMSPropOptimizer(learning_rate=params['lr'],
                                              momentum=0.9)
    elif params['optimizer'] == 'sgd':
        optimizer = tf.train.MomentumOptimizer(learning_rate=params['lr'],
                                               momentum=0.9,
                                               use_nesterov=True)
    elif params['optimizer'] == 'adam':
        optimizer = tf.train.AdamOptimizer(learning_rate=params['lr'])
    else:
        assert 'No optimizer defined in params!'

    train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())

    return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
testset = datasets.CIFAR10(root=data_location,
                           train=False,
                           download=True,
                           transform=test_transform)
testloader = torch.utils.data.DataLoader(testset,
                                         shuffle=False,
                                         batch_size=args.test_batch_size,
                                         **loader_args)

classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse',
           'ship', 'truck')

#model = AlexNet(num_classes=10, small_input=True, use_ttq=args.ttq)
model = SqueezeNet(version=1.1,
                   num_classes=10,
                   small_input=True,
                   use_ttq=args.ttq)
#model = resnet18(use_ttq=args.ttq)


def fix_state(valid, new):
    for k, v in valid.items():
        parts = k.split('.')
        if len(parts) > 1:
            if parts[-1] == 'W_p':
                if not k in new:
                    new[k] = torch.Tensor(1)
                    new[k][0] = valid['.'.join(parts[:-1] +
                                               ['weight'])].max() / 2
            elif parts[-1] == 'W_n':
                if not k in new:
Exemple #30
0
import numpy as np
from keras.applications.imagenet_utils import preprocess_input, decode_predictions
from keras.preprocessing import image
from squeezenet import SqueezeNet
import matplotlib.pyplot as plt
# import matplotlib.image as mpimg
model = SqueezeNet()

img = image.load_img('2.jpg', target_size=(227, 227))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)
all_results = decode_predictions(preds)
for results in all_results:
    for result in results:
        print('Probability %0.2f%% => [%s]' % (100 * result[2], result[1]))
        result_text = 'Probability %0.2f%% => [%s]' % (100 * result[2],
                                                       result[1])
        break

plt.figure(num=1, figsize=(8, 6), dpi=80)
plt.imshow(img)
plt.text(130,
         90,
         result_text,
         horizontalalignment='center',
         verticalalignment='center',
         fontsize=16,
         color='black')
Exemple #31
0
class DNNModel:
    def __init__(self, image_path):
        self.IMAGE_SIZE = 64
        self.data = []
        self.labels = []
        self.model = self.build_model()

        if image_path is not None:
            self.image_path = image_path
        else:
            self.image_path = "/home/madi/deeplearning/raspberry-pi/datasets"
        pass

    def gen_training_image_set(self):
        imagePaths = os.listdir(self.image_path)
        # loop over the input images
        for imagePath in imagePaths:
            # load the image, pre-process it, and store it in the data list
            imagePath = self.image_path + "/" + imagePath
            print imagePath
            image = cv2.imread(imagePath)
            image = cv2.resize(image, (self.IMAGE_SIZE, self.IMAGE_SIZE))
            image = img_to_array(image)

            self.data.append(image)

            # extract the class label from the image path and update the
            # labels list]
            if "left" in imagePath.split(os.path.sep)[-2]:
                label = 1
            elif "right" in imagePath.split(os.path.sep)[-2]:
                label = 2
            else:
                label = 0

            self.labels.append(label)

        # scale the raw pixel intensities to the range [0, 1]
        self.data = np.array(self.data, dtype="float") / 255.0
        self.labels = np.array(self.labels)

    def add_training_sample(self, data, label):
        image = cv2.resize(data, (self.IMAGE_SIZE, self.IMAGE_SIZE))
        image = img_to_array(image)
        self.data.append(image)
        self.labels.append(label)

    def scale_and_norm_training_samples(self):
        # scale the raw pixel intensities to the range [0, 1]
        self.data = np.array(self.data, dtype="float") / 255.0
        self.labels = np.array(self.labels)

    def build_model(self):
        self.model = SqueezeNet(include_top=True,
                                weights=None,
                                classes=3,
                                input_shape=(self.IMAGE_SIZE, self.IMAGE_SIZE,
                                             3))
        self.model.summary()
        opt = Adam()
        self.model.compile(loss="binary_crossentropy",
                           optimizer=opt,
                           metrics=["accuracy"])
        return self.model

    def train(self):
        # split train and test set
        (trainX, testX, trainY, testY) = train_test_split(self.data,
                                                          self.labels,
                                                          test_size=0.25,
                                                          random_state=42)

        # convert the labels from integers to vectors
        trainY = to_categorical(trainY, num_classes=3)
        testY = to_categorical(testY, num_classes=3)

        print trainX.shape
        print trainY.shape
        self.model.fit(trainX,
                       trainY,
                       batch_size=1,
                       epochs=50,
                       verbose=1,
                       validation_data=(testX, testY))
        self.test()
        pass

    def predict(self, img_frame):
        img_frame = cv2.resize(img_frame, (self.IMAGE_SIZE, self.IMAGE_SIZE))
        img_frame = img_to_array(img_frame)
        data = np.array([img_frame])
        # scale the raw pixel intensities to the range [0, 1]
        data = np.array(data, dtype="float") / 255.0

        ret = self.model.predict(data)

        if len(ret) > 0:
            return ret[0]
        pass

    def save_model(self):
        self.model.save("greenball_squeezenet_local.h5")
        pass

    def load_model(self, path):
        self.model = load_model("greenball_squeezenet_local.h5")
        pass

    def test(self):
        cnt = 0
        for i in xrange(len(self.data)):
            ret = self.model.predict(self.data[i])
            pred = np.argmax(ret)
            if pred == self.labels[i]:
                cnt += 1
        print "total correct number is %d" % cnt