各种深度学习框架实现猫狗大战

举报
悲恋花丶无心之人 发表于 2021/02/03 00:17:40 2021/02/03
【摘要】 目录 不同深度学习框架下的实现教程/github地址 1.Pytorch 2.TensorFlow 3.Keras 4.MXNet 不同深度学习框架下的实现教程/github地址(好用的话记得star噢) 1.Pytorch 一个教程和项目地址,代码需要自己建立项目,或者从github上下载 PyTorch 入门实战(五)——2013kaggle比赛...


目录

不同深度学习框架下的实现教程/github地址

1.Pytorch

2.TensorFlow

3.Keras

4.MXNet


不同深度学习框架下的实现教程/github地址(好用的话记得star噢)

1.Pytorch

一个教程和项目地址,代码需要自己建立项目,或者从github上下载

PyTorch 入门实战(五)——2013kaggle比赛 猫狗大战的实现

https://github.com/nickhuang1996/Dogs_vs_Cats_Pytorch

2.TensorFlow

个人感觉写的不是很好,但是也算完成了分类任务, 可以通过tensorboard查看损失和准确率变化

https://github.com/nickhuang1996/Dogs_vs_Cats_TensorFlow_No_Keras

3.Keras

利用TensorFlow中的Keras接口,代码比较完善,博主还花了很长的时间把用到的预训练网络改成Pytorch的形式

https://github.com/nickhuang1996/Dogs_vs_Cats_TensorFlow_Keras

例如resnet50:


  
  1. from tensorflow.python.keras.layers import Activation
  2. from tensorflow.python.keras.layers import AveragePooling2D
  3. from tensorflow.python.keras.layers import BatchNormalization
  4. from tensorflow.python.keras.layers import Conv2D
  5. from tensorflow.python.keras.layers import Dense
  6. from tensorflow.python.keras.layers import Flatten
  7. from tensorflow.python.keras.layers import GlobalAveragePooling2D
  8. from tensorflow.python.keras.layers import GlobalMaxPooling2D
  9. from tensorflow.python.keras.layers import Input
  10. from tensorflow.python.keras.layers import MaxPooling2D
  11. from tensorflow.python.keras.layers import ZeroPadding2D
  12. from tensorflow.python.keras import backend as K
  13. from tensorflow.python.keras import layers
  14. class conv_block(object):
  15. def __init__(self, kernel_size, filters, stage, block, strides=(2, 2)):
  16. filters1, filters2, filters3 = filters
  17. if K.image_data_format() == 'channels_last':
  18. bn_axis = 3
  19. else:
  20. bn_axis = 1
  21. conv_name_base = 'res' + str(stage) + block + '_branch'
  22. bn_name_base = 'bn' + str(stage) + block + '_branch'
  23. self.conv1 = Conv2D(filters1, (1, 1), strides=strides, name=conv_name_base + '2a')
  24. self.bn1 = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a')
  25. self.conv2 = Conv2D(filters2, kernel_size, padding='same', name=conv_name_base + '2b')
  26. self.bn2 = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b')
  27. self.conv3 = Conv2D(filters3, (1, 1), name=conv_name_base + '2c')
  28. self.bn3 = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c')
  29. self.shortcut_conv = Conv2D(filters3, (1, 1), strides=strides, name=conv_name_base + '1')
  30. self.shortcut_bn = BatchNormalization(axis=bn_axis, name=bn_name_base + '1')
  31. self.relu = Activation('relu')
  32. def __call__(self, input_tensor):
  33. x = self.conv1(input_tensor)
  34. x = self.bn1(x)
  35. x = self.relu(x)
  36. x = self.conv2(x)
  37. x = self.bn2(x)
  38. x = self.relu(x)
  39. x = self.conv3(x)
  40. x = self.bn3(x)
  41. shortcut = self.shortcut_conv(input_tensor)
  42. shortcut = self.shortcut_bn(shortcut)
  43. x = layers.add([x, shortcut])
  44. x = self.relu(x)
  45. return x
  46. class identity_block(object):
  47. def __init__(self, kernel_size, filters, stage, block):
  48. filters1, filters2, filters3 = filters
  49. if K.image_data_format() == 'channels_last':
  50. bn_axis = 3
  51. else:
  52. bn_axis = 1
  53. conv_name_base = 'res' + str(stage) + block + '_branch'
  54. bn_name_base = 'bn' + str(stage) + block + '_branch'
  55. self.conv1 = Conv2D(filters1, (1, 1), name=conv_name_base + '2a')
  56. self.bn1 = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a')
  57. self.conv2 = Conv2D(filters2, kernel_size, padding='same', name=conv_name_base + '2b')
  58. self.bn2 = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b')
  59. self.conv3 = Conv2D(filters3, (1, 1), name=conv_name_base + '2c')
  60. self.bn3 = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c')
  61. self.relu = Activation('relu')
  62. def __call__(self, input_tensor):
  63. x = self.conv1(input_tensor)
  64. x = self.bn1(x)
  65. x = self.relu(x)
  66. x = self.conv2(x)
  67. x = self.bn2(x)
  68. x = self.relu(x)
  69. x = self.conv3(x)
  70. x = self.bn3(x)
  71. x = layers.add([x, input_tensor])
  72. x = self.relu(x)
  73. return x
  74. class ResNet50(object):
  75. def __init__(self, include_top=True, classes=1000, pooling=None):
  76. if K.image_data_format() == 'channels_last':
  77. bn_axis = 3
  78. else:
  79. bn_axis = 1
  80. self.include_top = include_top
  81. self.pooling = pooling
  82. self.conv1 = Conv2D(64, (7, 7), strides=(2, 2), padding='same', name='conv1')
  83. self.bn1 = BatchNormalization(axis=bn_axis, name='bn_conv1')
  84. self.relu = Activation('relu')
  85. self.maxpool = MaxPooling2D((3, 3), strides=(2, 2))
  86. self.layer1_list = [
  87. conv_block(3, [64, 64, 256], stage=2, block='a', strides=(1, 1)),
  88. identity_block(3, [64, 64, 256], stage=2, block='b'),
  89. identity_block(3, [64, 64, 256], stage=2, block='c'),
  90. ]
  91. self.layer2_list = [
  92. conv_block(3, [128, 128, 512], stage=3, block='a'),
  93. identity_block(3, [128, 128, 512], stage=3, block='b'),
  94. identity_block(3, [128, 128, 512], stage=3, block='c'),
  95. identity_block(3, [128, 128, 512], stage=3, block='d'),
  96. ]
  97. self.layer3_list = [
  98. conv_block(3, [256, 256, 1024], stage=4, block='a'),
  99. identity_block(3, [256, 256, 1024], stage=4, block='b'),
  100. identity_block(3, [256, 256, 1024], stage=4, block='c'),
  101. identity_block(3, [256, 256, 1024], stage=4, block='d'),
  102. identity_block(3, [256, 256, 1024], stage=4, block='e'),
  103. identity_block(3, [256, 256, 1024], stage=4, block='f'),
  104. ]
  105. self.layer4_list = [
  106. conv_block(3, [512, 512, 2048], stage=5, block='a'),
  107. identity_block(3, [512, 512, 2048], stage=5, block='b'),
  108. identity_block(3, [512, 512, 2048], stage=5, block='c'),
  109. ]
  110. self.avgpool = AveragePooling2D((7, 7), name='avg_pool')
  111. self.flatten = Flatten()
  112. self.fc = Dense(classes, activation='softmax', name='fc1000')
  113. self.GAP = GlobalAveragePooling2D()
  114. self.GMP = GlobalMaxPooling2D()
  115. def layer1(self, x):
  116. for i in range(len(self.layer1_list)):
  117. x = self.layer1_list[i](x)
  118. return x
  119. def layer2(self, x):
  120. for i in range(len(self.layer2_list)):
  121. x = self.layer2_list[i](x)
  122. return x
  123. def layer3(self, x):
  124. for i in range(len(self.layer3_list)):
  125. x = self.layer3_list[i](x)
  126. return x
  127. def layer4(self, x):
  128. for i in range(len(self.layer4_list)):
  129. x = self.layer4_list[i](x)
  130. return x
  131. def __call__(self, img_input):
  132. x = self.conv1(img_input)
  133. x = self.bn1(x)
  134. x = self.relu(x)
  135. x = self.maxpool(x)
  136. x = self.layer1(x)
  137. x = self.layer2(x)
  138. x = self.layer3(x)
  139. x = self.layer4(x)
  140. x = self.avgpool(x)
  141. if self.include_top:
  142. x = self.flatten(x)
  143. x = self.fc(x)
  144. else:
  145. if self.pooling == 'avg':
  146. x = self.GAP(x)
  147. elif self.pooling == 'max':
  148. x = self.GMP(x)
  149. return x

4.MXNet

从头到尾写了一遍发现和TensorFlow和Pytorch都很相似,感觉用起来也很不错~

https://github.com/nickhuang1996/Dogs_vs_Cats_MXNet


希望以上教程和代码可以帮助到更多学习深度学习框架的人们!!

文章来源: nickhuang1996.blog.csdn.net,作者:悲恋花丶无心之人,版权归原作者所有,如需转载,请联系作者。

原文链接:nickhuang1996.blog.csdn.net/article/details/103644917

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。