当前位置:   article > 正文

残差网络ResNet50学习与训练多分类任务-365每周深度学习J1_多标签分类resnet50

多标签分类resnet50

目录

数据集下载

resnet网络讲解

应用resnet50解决多分类任务思路

导包

处理数据集

查看数据集

搭建模型

训练

展示训练结果


数据集下载

在我上传的资源中。点击头像并查看免费下载

resnet网络讲解

 残差网络的作用为解决梯度消失问题。如何通过激活函数后的梯度始终小于1,不妨设上限为0.9,则经过N层后的梯度会小于0.9^n,则在20后的梯度几乎都会小于0.1,最终当层数很大时就几乎等于零。而残杀网络则解决了网络不能深下去的问题。因为它在每一层计算梯度的都开了一个洞,它既可以通过这层,也可以不用这层。所以当这层对梯度消失影响很大时,就可以不用。

   

应用resnet50解决多分类任务思路

导包

处理数据集

查看数据集

搭建模型

训练

测试

导包

  1. import matplotlib.pyplot as plt
  2. plt.rcParams['font.sans-serif']=['SimHei']
  3. plt.rcParams['axes.unicode_minus']=False
  4. import os,PIL,pathlib
  5. import numpy as np
  6. from tensorflow import keras
  7. from tensorflow.keras import layers,models
  8. from utils.utils import *

处理数据集

  1. data_dir=r'E:\stu-ch\whale\tensorflow\resNet50\data\bird_photos'
  2. data_dir=pathlib.Path(data_dir)
  3. #%%
  4. image_count=len(list(data_dir.glob('*/*')))
  5. train_ds=tf.keras.preprocessing.image_dataset_from_directory(
  6. data_dir,
  7. validation_split=0.2,
  8. subset='training',
  9. seed=123,
  10. image_size=(int(config['img_height']),int(config['img_width'])),
  11. batch_size=int(config['batch_size'])
  12. )
  13. val_ds=tf.keras.preprocessing.image_dataset_from_directory(
  14. data_dir,
  15. validation_split=0.2,
  16. subset='validation',
  17. seed=123,
  18. image_size=(int(config['img_height']), int(config['img_width'])),
  19. batch_size=int(config['batch_size'])
  20. )

查看数据集

  1. #%%
  2. class_names=train_ds.class_names
  3. print(class_names)
  4. #%%
  5. plt.figure(figsize=(10,5))
  6. for images,labels in train_ds.take(1):
  7. for i in range(8):
  8. ax=plt.subplot(2,4,i+1)
  9. plt.imshow(images[i].numpy().astype('uint8'))
  10. plt.title(class_names[labels[i]])
  11. plt.axis('off')
  12. plt.show()
  13. #%%
  14. for image_batch,labels_batch in train_ds:
  15. print(image_batch.shape)
  16. print(labels_batch.shape)
  17. break
  18. #%%
  19. train_ds=train_ds.cache().shuffle(1000).prefetch(buffer_size=tf.data.AUTOTUNE)
  20. val_ds=val_ds.cache().prefetch(buffer_size=tf.data.AUTOTUNE)

搭建模型

  1. #%%
  2. from tensorflow.keras import layers
  3. from tensorflow.keras.layers import Input,Activation,BatchNormalization,Flatten
  4. from tensorflow.keras.layers import Dense,Conv2D,MaxPooling2D,ZeroPadding2D,AveragePooling2D
  5. from tensorflow.keras.models import Model
  6. def identity_block(input_tensor,kernel_size,filters,stage,block):
  7. filters1,filters2,filters3=filters
  8. name_base=str(stage)+block+'_identity_block_'
  9. x=Conv2D(filters1,(1,1),name=name_base+'conv1')(input_tensor)
  10. x=BatchNormalization(name=name_base+'bn1')(x)
  11. x=Activation('relu',name=name_base+'relu1')(x)
  12. x=Conv2D(filters2,kernel_size,padding='same',name=name_base+'conv2')(x)
  13. x=BatchNormalization(name=name_base+'bn2')(x)
  14. x=Activation('relu',name=name_base+'relu2')(x)
  15. x=Conv2D(filters3,(1,1),name=name_base+'conv3')(x)
  16. x=BatchNormalization(name=name_base+'bn3')(x)
  17. x=layers.add([x,input_tensor],name=name_base+'add')
  18. x=Activation('relu',name=name_base+'relu4')(x)
  19. return x
  20. def conv_block(input_tensor, kernel_size, filters, stage, block,strides=(2,2)):
  21. filters1, filters2, filters3 = filters
  22. res_name_base=str(stage)+block+"_conv_block_res_"
  23. name_base = str(stage) + block + '_conv_block_'
  24. x = Conv2D(filters1, (1, 1), strides=strides,name=name_base + 'conv1')(input_tensor)
  25. x = BatchNormalization(name=name_base + 'bn1')(x)
  26. x = Activation('relu', name=name_base + 'relu1')(x)
  27. x = Conv2D(filters2, kernel_size,padding='same' ,name=name_base + 'conv2')(x)
  28. x = BatchNormalization(name=name_base + 'bn2')(x)
  29. x = Activation('relu', name=name_base + 'relu2')(x)
  30. x = Conv2D(filters3, (1, 1), name=name_base + 'conv3')(x)
  31. x = BatchNormalization(name=name_base + 'bn3')(x)
  32. x2 = Conv2D(filters3, (1, 1), strides=strides,name=name_base + 'conv')(input_tensor)
  33. x2 = BatchNormalization(name=res_name_base + 'bn')(x2)
  34. x = layers.add([x, x2], name=name_base + 'add')
  35. x = Activation('relu', name=name_base + 'relu4')(x)
  36. return x
  37. def resnet50(input_shape=[224,224,3],classes=1000):
  38. img_input=Input(shape=input_shape)
  39. x=ZeroPadding2D((3,3))(img_input)
  40. x=Conv2D(64,(7,7),strides=(2,2),name='conv1')(x)
  41. x=BatchNormalization(name='bn_conv1')(x)
  42. x=Activation('relu')(x)
  43. x=MaxPooling2D((3,3),strides=(2,2))(x)
  44. x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
  45. x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
  46. x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')
  47. x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
  48. x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
  49. x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
  50. x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')
  51. x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
  52. x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
  53. x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
  54. x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
  55. x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
  56. x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')
  57. x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
  58. x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
  59. x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')
  60. x = AveragePooling2D((7, 7), name='avg_pool')(x)
  61. x = Flatten()(x)
  62. x = Dense(classes, activation='softmax', name='fc1000')(x)
  63. model = Model(img_input, x, name='resnet50')
  64. # 加载预训练模型
  65. model.load_weights(r"data\resnet50_weights_tf_dim_ordering_tf_kernels.h5")
  66. return model
  67. model = resnet50()
  68. model.summary()

训练

  1. opt=tf.keras.optimizers.Adam(learning_rate=1e-7)
  2. model.compile(optimizer='adam',
  3. loss='sparse_categorical_crossentropy',
  4. metrics=['accuracy'])
  5. #%%
  6. epochs=20
  7. history=model.fit(
  8. train_ds,
  9. validation_data=val_ds,
  10. epochs=epochs
  11. )

展示训练结果

  1. acc = history.history['accuracy']
  2. val_acc = history.history['val_accuracy']
  3. loss = history.history['loss']
  4. val_loss = history.history['val_loss']
  5. epochs_range = range(epochs)
  6. plt.figure(figsize=(12, 4))
  7. plt.subplot(1, 2, 1)
  8. plt.suptitle("csdn:howard_DL")
  9. plt.plot(epochs_range, acc, label='Training Accuracy')
  10. plt.plot(epochs_range, val_acc, label='Validation Accuracy')
  11. plt.legend(loc='lower right')
  12. plt.title('Training and Validation Accuracy')
  13. plt.subplot(1, 2, 2)
  14. plt.plot(epochs_range, loss, label='Training Loss')
  15. plt.plot(epochs_range, val_loss, label='Validation Loss')
  16. plt.legend(loc='upper right')
  17. plt.title('Training and Validation Loss')
  18. plt.show()

结果

 

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/372784
推荐阅读
相关标签
  

闽ICP备14008679号