Kaggle Invasive Species Detection VGG16 example--based on Keras

Source: Internet
Author: User
Tags shuffle keras keras model

According to the description of the kaggle:invasive species monitoring problem, we need to judge whether the image contains invasive species, that is, to classify the images (0: No invasive species in the image; 1: The images contain invasive species), According to the data given (2295 graphs and categories of the training set, 1531 graphs of the test set), it is clear that this kind of image classification task is very suitable to be solved by CNN, KERA Application Module application provides Keras model with pre-training weights, such as Xception, VGG16, VGG19, ResNet50, InceptionV3 (Tensorlow back-end only), these models can be used for prediction, feature extraction, and finetune. And according to the "bottleneck" feature of these models, we can directly load the pre-trained model and reduce the training cost without affecting the accuracy of CNN, which is convenient and quick. For demonstration purposes, this article only demonstrates the VGG16 model.

First, import the libraries that require preprocessing.

Import OS
import NumPy as NP
import pandas as PD
import h5py
import matplotlib.pyplot as Plt
%matplot Lib inline

trainpath = str (' e:\\kaggle\invasive_species\\train\\ ')
testpath = str (' E:\\kaggle\\invasive_ Species\\test\\ ')
n_tr = Len (Os.listdir (trainpath))
print (' num of training files: ', n_tr)

Num of training files:2295

You can see the specifics of the train_labels.csv, which is shown in the table below, where the data is already scrambled, and the samples labeled 0 and 1 are randomly arranged.

Train_labels = pd.read_csv (' e:\\kaggle\invasive_species\\train_labels.csv ')
train_labels.head ()
name Invasive
0 1 0
1 2 0
2 3 1
3 4 0
4 5 1

You can visualize the images of the samples labeled 0 and 1, respectively, to see what they look like:

From skimage import io, transform

sample_image = io.imread (Trainpath + ' 1.jpg ')
print (' height:{0} width:{1} '. Format (*sample_image.shape))
plt.imshow (sample_image)

height:866 width:1154
<matplotlib.image.axesimage at 0x1f1b54f82b0>

Sample_image = Io.imread (Trainpath + ' 3.jpg ')
plt.imshow (sample_image)

# There is one image in the test set that H As reversed dimensions.
# Print (Io.imread (Testpath + ' 1068.jpg '). Shape)

<matplotlib.image.axesimage at 0x25073e4d208>

As shown in the above two figure, the invasive species is characterized by: a ball-like white flower and a more generous leaf.

It can also be noted that the image pixel is more, and the test set has a picture of the width and height ratio is reversed, it is necessary to transform the size of the image, so that it is suitable for the existing CNN algorithm.

We can use Scikit-image's Tranform method to transform the image so that the resulting image will have a pixel value between 0 and 1, and also use Np.random.permutation () to shuffle the order of the image, Avoid the artificial impact of unknown raw data distribution.

x = Np.empty (shape= (N_TR, 224, 224, 3))
y = np.empty (n_tr)
labels = train_labels.invasive.values for

k,v in EN Umerate (Np.random.permutation (N_TR)):
    path = ' {0}{1}.jpg '. Format (Trainpath, v+1)
    tr_im = io.imread (path)
    X[k] = transform.resize (Tr_im, output_shape= (224, 224, 3))
    y[k] = labels[v]

x = X.astype (' float32 ')  # elements in X is between 0 and 1 inclusively.

When the data transformation is completed, it can be saved, so that in subsequent calls will be convenient, much faster, especially when the amount of data is large, more should be the case.

# Save as h5 file
f = h5py. File (' E:\\kaggle\invasive_species\\ndarray_train.h5 ', ' W ')
f[' x ']=x
f[' y ']=y
f.close ()

When reading the data, note that the end of the [:] cannot be omitted.

# read h5 file
f = h5py. File (' E:\\kaggle\invasive_species\\ndarray_train.h5 ', ' R ')
x = f[' x '] [:]  # F.keys () can see all primary keys
y = f[' y '] [ :]
F.close ()

Next, you can use Scikit-learn's train_test_split to divide the training set and the validation set directly.

From sklearn.model_selection import train_test_split

x_train, X_val, y_train, Y_val = Train_test_split (x, Y, Test_ size=0.2)
print (x_train.shape,y_train.shape,x_val.shape,y_val.shape, sep= ' \ n ')

(1836, 224, 224, 3)
(1836,)
(459, 224, 224, 3)
(459,)

Once the data preprocessing is complete, you can start building the CNN Network. First, import a VGG16 model that does not contain a fully connected layer (the first run automatically downloads the weights contained in the model, and the function is downloaded directly from GitHub, which may be slower).

From keras.models import sequential, Model from
keras import applications from
keras.layers import dropout, Flatt En, dense from
keras.optimizers import SGD

Img_shape = (224, 224, 3)

Base_model = applications. VGG16 (weights= ' imagenet ', Include_top=false, Input_shape=img_shape)

Using TensorFlow backend.

Next we build our own fully-connected layer and set the convolution weight factor to not participate in the training and complete the model compilation.

Add_model = Sequential ()
Add_model.add (Flatten (input_shape=base_model.output_shape[1:]))
Add_model.add ( Dense (activation= ' relu '))
add_model.add (Dense (1, activation= ' sigmoid '))

model = Model (inputs=base_ Model.input, Outputs=add_model (base_model.output)) for the

layer in model.layers[:-1]:
    layer.trainable = False

model.compile (loss= ' binary_crossentropy ', OPTIMIZER=SGD (lr=1e-4, momentum=0.9), metrics=[' accuracy ')

The specific structure of the CNN model that we built can be directly viewed as follows:

Model.summary ()
_________________________________________________________________ Layer (type) Output Shape P Aram # ================================================================= Input_1 (InputLayer) (None, 224, 224, 3) 0 _________________________________________________________________ block1_conv1 (Conv2D) (None,        224, 224, 1792 _________________________________________________________________ block1_conv2 (Conv2D) (None, 224, 224, up) 36928 _________________________________________________________________ Block1_pool (Max Pooling2d) (None, 0, _________________________________________________________________ Block2 _CONV1 (conv2d) (None, 112, 112, 128) 73856 ______________________________________________________________ ___ Block2_conv2 (conv2d) (None, 112, 112, 128) 147584 ____________________________________________________ _____________ Block2_pooL (maxpooling2d) (None, 56, 56, 128) 0 _________________________________________________________________ BLOCK3_CONV1 (conv2d) (None, 56, 56, 256) 295168 ________________________________________________________ _________ Block3_conv2 (conv2d) (None, 56, 56, 256) 590080 ______________________________________________ ___________________ block3_conv3 (conv2d) (None, 56, 56, 256) 590080 ____________________________________ _____________________________ Block3_pool (maxpooling2d) (None, 28, 28, 256) 0 __________________________ _______________________________________ Block4_conv1 (conv2d) (None, 28, 28, 512) 1180160 ________________ _________________________________________________ Block4_conv2 (conv2d) (None, 28, 28, 512) 2359808 ___    
___________________________________________________________ block4_conv3 (conv2d) (None, 28, 28, 512) 2359808 _________________________________________________________________ Block4_pool (maxpooling2d) (None, 14, 14, 512) 0 __________   
_______________________________________________________ Block5_conv1 (conv2d) (None, 14, 14, 512) 2359808 _________________________________________________________________ Block5_conv2 (Conv2D) (None, 14, 14, 512) 2 359808 _________________________________________________________________ Block5_conv3 (Conv2D) (None, 14, 14, 51 2) 2359808 _________________________________________________________________ Block5_pool (MaxPooling2D) (None,    7, 7, 0 _________________________________________________________________ sequential_1 (sequential) (None, 1) 6423041 ================================================================= Total params:21 , 137,729 trainable params:6,423,041 non-trainable params:14,714,688 _______________________________________________
 __________________

Next is the more important part, because our data volume is not large enough, if the direct repetition training may improve performance and not good, so we need to use imagedatagenerator for real-time data promotion, it by the given data to scale, pan, cut, flip, Rotation and other operations to produce new images, but also to the input image to be centralized, standardized and other operations. In this case, the data should be centralized, or the subsequent training process will encounter the gradient disappears, the model accuracy rate can not be gradually improved. To save time (poor notebook performance), only 2 rounds were iterated, and the accuracy rate on the test set reached 86%.

# epochs for demonstration purposes from Keras.preprocessing.image import Imagedatagenerator From keras.callbacks import modelcheckpoint batch_size = ten epochs = 2 Train_datagen = Imagedatagenerator (featurewise_ce Nter=true, rotation_range=30, zoom_range=0.2, width_shift_range=0.1, Height_shift_range =0.1, Horizontal_flip=true, fill_mode= ' nearest ') Val_datagen = Imagedatagenerator (featurewise_center=true) train_ Datagen.fit (X_train) val_datagen.fit (x_val) train_datagenerator = Train_datagen.flow (X_train, Y_train, batch_size= batch_size) Validation_generator = Val_datagen.flow (X_val, Y_val, batch_size=batch_size) history = Model.fit_generator (Train_datagenerator, steps_per_epoch=x_train.shape[0]//Batch_size, Epochs=epochs, Validation_data=vali Dation_generator, Validation_steps=50, Callbacks=[modelcheckpoint (' Vgg16-transferlearning.model ', monitor= ' Val_ac C ', save_best_only=true)]) 

Epoch 1/2
183/183 [==============================]-780s-loss:0.5579-acc:0.7038-val_loss:0.4187-val_acc:0.8156
Epoch 2/2
183/183 [==============================]-792s-loss:0.4199-acc:0.7978-val_loss:0.3384-val_acc:0.8637 Pred ICT the test set

Next, the test set data is forecasted. It is important to note that in forecasting, you need to make the test set's data generator consistent with the validation set, because the de-centering of the validation set is based on the validation set itself, so the test set should also be centralized with the data of the validation set.

From skimage import io, transform

n_test = Len (Os.listdir (Testpath))
xx = Np.empty (shape= (n_test, 224, 224, 3)) 
  xx = Xx.astype (' float32 ') for

I in range (n_test):
    path = ' {0}{1}.jpg '. Format (Testpath, i+1)
    Test_im = Io.imread (path)
    xx[i] = transform.resize (Test_im, output_shape= (224, 224, 3))

# Save as h5 file
f = h5py. File (' E:\\kaggle\invasive_species\\ndarray_test.h5 ', ' W ')
f[' x ']=xx
f.close ()
# read h5 file
f = h5py. File (' E:\\kaggle\invasive_species\\ndarray_test.h5 ', ' R ')
x_test = f[' x ' [:]  # F.keys () can see all the primary keys
F.close ()
test_generator = Val_datagen.flow (X_test, batch_size=1, Shuffle=false)
result = Model.predict_generator (Test_generator, N_test)

You can then convert the probability of the output [0,1] to a 0,1 tag and write to the CSV file.

result[result>0.5] = 1
result[result!=1] = 0
result[0]

Array ([1.], Dtype=float32)

DF = pd.read_csv (' e:\\kaggle\invasive_species\\sample_submission.csv ')
df.invasive = Result.flatten ()
Df.head ()
name Invasive
0 1 1.0
1 2 0.0
2 3 0.0
3 4 0.0
4 5 1.0
Df.to_csv (' E:\\kaggle\invasive_species\\demo_submission.csv ', index=false)

By submitting it to Kaggle, the public lb of the test set is about 84%, indicating that the model has not been fitted.

Based on this, we can try more data processing methods, model super-parameter adjustment, different CNN model comparison, integration, and the use of cross-validation to further improve.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.