1.导入必要的库
#Import some necessary Modules
import os
import cv2
import keras
import numpy as np
import pandas as pd
import random as rn
from PIL import Image
from tqdm import tqdm
import matplotlib.pyplot as plt
from IPython.display import SVG
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import LabelEncoder
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.optimizers import Adam
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.utils import to_categorical
from tensorflow.python.keras.callbacks import ReduceLROnPlateau
from tensorflow.python.keras.utils.vis_utils import model_to_dot
from tensorflow.python.keras.applications.vgg16 import VGG16
from tensorflow.python.keras.applications.resnet50 import ResNet50,preprocess_input
from sklearn.model_selection import train_test_split,KFold, cross_val_score, GridSearchCV
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator,load_img, img_to_array
from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D,BatchNormalization,Dropout,Conv2D,MaxPool2D
#Resnet-50 has been pre_trained, weights have been saved in below path
resnet_weights_path = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
vgg16_weights_path="../input/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels.h5"
#Display the dir list
print(os.listdir("../input"))
2.输出结果:
Using TensorFlow backend.
['skin-cancer-malignant-vs-benign', 'vgg16', 'resnet50']
将JPG文件转化为数组
def Dataset_loader(DIR,RESIZE):
IMG = []
read = lambda imname: np.asarray(Image.open(imname).convert("RGB"))
for IMAGE_NAME in tqdm(os.listdir(DIR)):
PATH = os.path.join(DIR,IMAGE_NAME)
_, ftype = os.path.splitext(PATH)
if ftype == ".jpg":
img = read(PATH)
img = cv2.resize(img, (RESIZE,RESIZE))
IMG.append(np.array(img)/255.)
return IMG
benign_train = np.array(Dataset_loader('../input/skin-cancer-malignant-vs-benign/data/train/benign',224))
malign_train = np.array(Dataset_loader('../input/skin-cancer-malignant-vs-benign/data/train/malignant',224))
benign_test = np.array(Dataset_loader('../input/skin-cancer-malignant-vs-benign/data/test/benign',224))
malign_test = np.array(Dataset_loader('../input/skin-cancer-malignant-vs-benign/data/test/malignant',224))
输出结果:
100%|██████████| 1440/1440 [00:07<00:00, 202.51it/s]
100%|██████████| 1197/1197 [00:05<00:00, 228.46it/s]
100%|██████████| 360/360 [00:01<00:00, 202.07it/s]
100%|██████████| 300/300 [00:01<00:00, 226.01it/s]
3.数据预处理
# Create labels
# Merge data
# Shuffle train data
# Split validation data from train data
# Shuffle test data
4.预览前12张图片
# Display first 15 images of moles, and how they are classified
w=60
h=40
fig=plt.figure(figsize=(15, 15))
columns = 4
rows = 3
for i in range(1, columns*rows +1):
ax = fig.add_subplot(rows, columns, i)
if Y_train[i] == 0:
ax.title.set_text('Benign')
else:
ax.title.set_text('Malignant')
plt.imshow(x_train[i], interpolation='nearest')
plt.show()
5.数据增强
# Data auguments
datagen = ImageDataGenerator(
featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
6.定义模型
# Define model with different applications
model = Sequential()
model.add(ResNet50(include_top=False,input_tensor=None,input_shape=(224,224,3),pooling='avg',classes=2,weights=resnet_weights_path))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
……
model.add(BatchNormalization())
model.add(Dense(1, activation='sigmoid'))
model.layers[0].trainable = False
model.summary()
输出结果:
Layer (type) Output Shape Param #
resnet50 (Model) (None, 2048) 23587712
flatten (Flatten) (None, 2048) 0
dense (Dense) (None, 512) 1049088
dropout (Dropout) (None, 512) 0
batch_normalization_v1 (Batc (None, 512) 2048
dense_1 (Dense) (None, 256) 131328
dropout_1 (Dropout) (None, 256) 0
batch_normalization_v1_1 (Ba (None, 256) 1024
dense_2 (Dense) (None, 1) 257
Total params: 24,771,457
Trainable params: 1,182,209
Non-trainable params: 23,589,248
7.编译模型
# Compile model
model.compile()
8.学习率衰减
#Learning rate decay with ReduceLROnPlateau
red_lr=
9.训练模型
# Train model
batch_size=64
epochs=150
History = model.fit_generator( )
输出结果:
……
Epoch 00146: ReduceLROnPlateau reducing learning rate to 9.095435737904722e-10.
26/26 [==============================] - 18s 686ms/step - loss: 0.1139 - acc: 0.9566 - val_loss: 0.2779 - val_acc: 0.8890
Epoch 147/150
1000/1000 [==============================] - 3s 3ms/sample - loss: 0.2764 - acc: 0.8890
26/26 [==============================] - 18s 687ms/step - loss: 0.1370 - acc: 0.9469 - val_loss: 0.2784 - val_acc: 0.8890
Epoch 148/150
1000/1000 [==============================] - 3s 3ms/sample - loss: 0.2761 - acc: 0.8890
26/26 [==============================] - 18s 704ms/step - loss: 0.1363 - acc: 0.9469 - val_loss: 0.2782 - val_acc: 0.8890
Epoch 149/150
1000/1000 [==============================] - 3s 3ms/sample - loss: 0.2760 - acc: 0.8890
Epoch 00149: ReduceLROnPlateau reducing learning rate to 6.366804861102082e-10.
26/26 [==============================] - 18s 693ms/step - loss: 0.1273 - acc: 0.9462 - val_loss: 0.2780 - val_acc: 0.8890
Epoch 150/150
1000/1000 [==============================] - 3s 3ms/sample - loss: 0.2754 - acc: 0.8900
26/26 [==============================] - 18s 689ms/step - loss: 0.1414 - acc: 0.9462 - val_loss: 0.2774 - val_acc: 0.8900
10.测试模型
# Testing model on test data to evaluate
lists=[]
y_pred = model.predict(X_test)
for i in range(len(y_pred)):
if y_pred[i][0]>0.5:
lists.append(1)
else:
lists.append(0)
print(accuracy_score(Y_test, lists))
输出结果:
0.8787878787878788
11.画图
plt.plot(History.history['acc'])
plt.plot(History.history['val_acc'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epochs')
plt.legend(['train', 'test'])
plt.show()
输出结果:
12.显示前8个良性图片
# Display first 8 images of benign
w=60
h=40
fig=plt.figure(figsize=(18, 10))
columns = 4
rows = 2
def Transfername(namecode):
if namecode==0:
return "Benign"
else:
return "Malignant"
for i in range(len(prop_class)):
ax = fig.add_subplot(rows, columns, i+1)
ax.set_title("Predicted result:"+ Transfername(lists[prop_class[i]])
+"\n"+"Actual result: "+ Transfername(Y_test[prop_class[i]]))
plt.imshow(X_test[prop_class[i]], interpolation='nearest')
plt.show()
输出结果:
13.显示前8个恶性图片
# Display first 8 images of benign
w=60
h=40
fig=plt.figure(figsize=(18, 10))
columns = 4
rows = 2
for i in range(len(mis_class)):
ax = fig.add_subplot(rows, columns, i+1)
ax.set_title("Predicted result:"+ Transfername(lists[mis_class[i]])
+"\n"+" Actual result: "+ Transfername(Y_test[mis_class[i]]))
plt.imshow(X_test[mis_class[i]], interpolation='nearest')
plt.show()</pre>
输出结果: