美文网首页
使用VGG16来训练车辆识别

使用VGG16来训练车辆识别

作者: 程序猿Cyinen | 来源:发表于2020-10-12 07:18 被阅读0次

    使用VGG16来训练车辆识别

    from keras.applications import VGG16
    conv_base = VGG16(weights='imagenet',
                        include_top=False,
                        input_shape=(150,150,3))
    
    
    import os 
    import numpy as np 
    from keras.preprocessing.image import ImageDataGenerator
    
    base_dir = '/Users/chenyin/Documents/深度学习/car'
    train_dir = os.path.join(base_dir,'train')
    validation_dir = os.path.join(base_dir,'val')
    test_dir = os.path.join(base_dir,'test')
    
    from keras import models
    from keras import layers
    from keras import optimizers
    

    加入分类层

    model = models.Sequential()
    model.add(conv_base)
    model.add(layers.Flatten())
    model.add(layers.Dense(256,activation='relu'))
    model.add(layers.Dense(10, activation='sigmoid'))
    
    model.summary()
    
    Model: "sequential"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    vgg16 (Model)                (None, 4, 4, 512)         14714688  
    _________________________________________________________________
    flatten (Flatten)            (None, 8192)              0         
    _________________________________________________________________
    dense (Dense)                (None, 256)               2097408   
    _________________________________________________________________
    dense_1 (Dense)              (None, 10)                2570      
    =================================================================
    Total params: 16,814,666
    Trainable params: 16,814,666
    Non-trainable params: 0
    _________________________________________________________________
    
    len(model.trainable_weights)
    
    30
    
    conv_base.trainable = False
    len(model.trainable_weights)
    
    4
    
    train_datagen = ImageDataGenerator(
        rescale=1./255,
        rotation_range=40,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True,
        fill_mode='nearest'
    )
    test_datagen = ImageDataGenerator(rescale=1./255)
    train_generator = train_datagen.flow_from_directory(
        train_dir,
        target_size=(150,150),
        batch_size=20,
        class_mode='binary'
    )
    validation_generator = test_datagen.flow_from_directory(
        validation_dir,
        target_size=(150,150),
        batch_size=20,
        class_mode='binary'
    )
    model.compile(loss='sparse_categorical_crossentropy',
        optimizer=optimizers.RMSprop(lr=2e-5),
        metrics=['acc'])
    
    Found 1400 images belonging to 10 classes.
    Found 200 images belonging to 10 classes.
    
    history = model.fit(
        train_generator,
        steps_per_epoch=100,
        epochs=30,
        validation_data=validation_generator,
        validation_steps=50
    )
    
    Epoch 1/30
    WARNING:tensorflow:AutoGraph could not transform <function Model.make_train_function.<locals>.train_function at 0x7fdfaeef3710> and will run it as-is.
    Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
    Cause: Bad argument number for Name: 4, expecting 3
    To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
    WARNING: AutoGraph could not transform <function Model.make_train_function.<locals>.train_function at 0x7fdfaeef3710> and will run it as-is.
    Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
    Cause: Bad argument number for Name: 4, expecting 3
    To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
    100/100 [==============================] - ETA: 0s - loss: 2.2270 - acc: 0.2045WARNING:tensorflow:AutoGraph could not transform <function Model.make_test_function.<locals>.test_function at 0x7fdfaed7aa70> and will run it as-is.
    Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
    Cause: Bad argument number for Name: 4, expecting 3
    To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
    WARNING: AutoGraph could not transform <function Model.make_test_function.<locals>.test_function at 0x7fdfaed7aa70> and will run it as-is.
    Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
    Cause: Bad argument number for Name: 4, expecting 3
    To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
    100/100 [==============================] - 199s 2s/step - loss: 2.2270 - acc: 0.2045 - val_loss: 2.0976 - val_acc: 0.3400
    Epoch 2/30
    100/100 [==============================] - 194s 2s/step - loss: 1.9879 - acc: 0.3965 - val_loss: 1.8054 - val_acc: 0.5260
    Epoch 3/30
    100/100 [==============================] - 198s 2s/step - loss: 1.7472 - acc: 0.5060 - val_loss: 1.5587 - val_acc: 0.5880
    Epoch 4/30
    100/100 [==============================] - 178s 2s/step - loss: 1.5686 - acc: 0.5600 - val_loss: 1.3564 - val_acc: 0.6710
    Epoch 5/30
    100/100 [==============================] - 179s 2s/step - loss: 1.4308 - acc: 0.5940 - val_loss: 1.2241 - val_acc: 0.6850
    Epoch 6/30
    100/100 [==============================] - 185s 2s/step - loss: 1.3169 - acc: 0.6170 - val_loss: 1.0816 - val_acc: 0.7150
    Epoch 7/30
    100/100 [==============================] - 179s 2s/step - loss: 1.2277 - acc: 0.6530 - val_loss: 1.0170 - val_acc: 0.7210
    Epoch 8/30
    100/100 [==============================] - 178s 2s/step - loss: 1.1479 - acc: 0.6790 - val_loss: 0.9509 - val_acc: 0.7530
    Epoch 9/30
    100/100 [==============================] - 179s 2s/step - loss: 1.0915 - acc: 0.6845 - val_loss: 0.8450 - val_acc: 0.7820
    Epoch 10/30
    100/100 [==============================] - 177s 2s/step - loss: 1.0540 - acc: 0.6915 - val_loss: 0.8223 - val_acc: 0.7720
    Epoch 11/30
    100/100 [==============================] - 182s 2s/step - loss: 0.9687 - acc: 0.7200 - val_loss: 0.7857 - val_acc: 0.7700
    Epoch 12/30
    100/100 [==============================] - 179s 2s/step - loss: 0.9658 - acc: 0.7180 - val_loss: 0.7357 - val_acc: 0.7820
    Epoch 13/30
    100/100 [==============================] - 178s 2s/step - loss: 0.9198 - acc: 0.7295 - val_loss: 0.7101 - val_acc: 0.7780
    Epoch 14/30
    100/100 [==============================] - 181s 2s/step - loss: 0.8924 - acc: 0.7425 - val_loss: 0.6926 - val_acc: 0.7940
    Epoch 15/30
    100/100 [==============================] - 179s 2s/step - loss: 0.8465 - acc: 0.7480 - val_loss: 0.6716 - val_acc: 0.7920
    Epoch 16/30
    100/100 [==============================] - 178s 2s/step - loss: 0.8407 - acc: 0.7435 - val_loss: 0.6581 - val_acc: 0.7780
    Epoch 17/30
    100/100 [==============================] - 176s 2s/step - loss: 0.8111 - acc: 0.7490 - val_loss: 0.5969 - val_acc: 0.7950
    Epoch 18/30
    100/100 [==============================] - 182s 2s/step - loss: 0.7752 - acc: 0.7705 - val_loss: 0.5955 - val_acc: 0.8000
    Epoch 19/30
    100/100 [==============================] - 176s 2s/step - loss: 0.7615 - acc: 0.7735 - val_loss: 0.5915 - val_acc: 0.8010
    Epoch 20/30
    100/100 [==============================] - 178s 2s/step - loss: 0.7400 - acc: 0.7790 - val_loss: 0.5745 - val_acc: 0.8060
    Epoch 21/30
    100/100 [==============================] - 175s 2s/step - loss: 0.7564 - acc: 0.7735 - val_loss: 0.5657 - val_acc: 0.7950
    Epoch 22/30
    100/100 [==============================] - 180s 2s/step - loss: 0.7097 - acc: 0.7865 - val_loss: 0.5864 - val_acc: 0.7930
    Epoch 23/30
    100/100 [==============================] - 179s 2s/step - loss: 0.7047 - acc: 0.7930 - val_loss: 0.5537 - val_acc: 0.8150
    Epoch 24/30
    100/100 [==============================] - 175s 2s/step - loss: 0.6993 - acc: 0.7830 - val_loss: 0.5388 - val_acc: 0.7970
    Epoch 25/30
    100/100 [==============================] - 178s 2s/step - loss: 0.6872 - acc: 0.7900 - val_loss: 0.5653 - val_acc: 0.8100
    Epoch 26/30
    100/100 [==============================] - 180s 2s/step - loss: 0.6430 - acc: 0.8040 - val_loss: 0.5413 - val_acc: 0.7990
    Epoch 27/30
    100/100 [==============================] - 176s 2s/step - loss: 0.6613 - acc: 0.8005 - val_loss: 0.5245 - val_acc: 0.8290
    Epoch 28/30
    100/100 [==============================] - 177s 2s/step - loss: 0.6185 - acc: 0.8040 - val_loss: 0.5202 - val_acc: 0.8300
    Epoch 29/30
    100/100 [==============================] - 178s 2s/step - loss: 0.6425 - acc: 0.8085 - val_loss: 0.5097 - val_acc: 0.8150
    Epoch 30/30
    100/100 [==============================] - 178s 2s/step - loss: 0.6132 - acc: 0.8145 - val_loss: 0.5098 - val_acc: 0.8410
    
    model.save("./car_Vgg16.h5")#保存模型
    
    import matplotlib.pyplot as plt
    
    
    acc = history.history['acc']
    
    val_acc = history.history['val_acc']
    
    loss = history.history['loss']
    
    val_loss = history.history['val_loss']
    epochs = range(1, len(acc) + 1)
    plt.plot(epochs, acc, 'bo', label='Training acc') 
    plt.plot(epochs, val_acc, 'b', label='Validation acc') 
    plt.title('Training and validation accuracy') 
    plt.legend()
    
    plt.figure()
    
    plt.plot(epochs, loss, 'bo', label='Training loss') 
    plt.plot(epochs, val_loss, 'b', label='Validation loss') 
    plt.title('Training and validation loss') 
    plt.legend()
    
    plt.show()
    
    test_list=os.listdir(test_dir)
    #train_generator.class_indices.keys()   标签类别
    labels=['SUV', 'bus', 'family sedan', 'fire engine', 'heavy truck', 'jeep', 'minibus', 'racing car', 'taxi', 'truck']
    
    from keras.preprocessing import image
    img_path=os.path.join(test_dir,test_list[180])
    img = image.load_img(img_path,target_size=(150,150))
    img.show()
    x=image.img_to_array(img)
    x=np.expand_dims(x,axis=0)
    x=x/255.0
    labels[model.predict_classes(x)[0]]
    
    'jeep'
    
    len(test_list)
    
    200
    
    
    

    相关文章

      网友评论

          本文标题:使用VGG16来训练车辆识别

          本文链接:https://www.haomeiwen.com/subject/yvwspktx.html