model.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Jun 21 10:53:51 2022
  4. model
  5. @author: fangjiasheng
  6. """
  7. import os
  8. import sys
  9. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  10. from isr.post_process import yolo_eval
  11. import time
  12. from functools import wraps
  13. from keras.layers import Lambda, Dense, Reshape, Conv2D, BatchNormalization, LeakyReLU, Masking, MaxPool2D, \
  14. MaxPooling2D, UpSampling2D, concatenate, Concatenate, Layer, GlobalAveragePooling2D, Multiply
  15. from keras import layers, models, Sequential, Input, Model
  16. import keras.backend as K
  17. import tensorflow as tf
  18. import numpy as np
  19. from keras.regularizers import l2
  20. from isr.utils import compose
  21. from tensorflow.python.framework import ops
  22. def seal_model_se(input_shape, output_shape, cls_num=3):
  23. inputs = Input(shape=input_shape)
  24. use_bias = False
  25. # # 256
  26. # down0 = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(inputs)
  27. # down0 = BatchNormalization()(down0)
  28. # down0 = LeakyReLU(alpha=0.1)(down0)
  29. # down0 = Conv2D(16, (1, 1), padding='same', use_bias=use_bias)(down0)
  30. # down0 = BatchNormalization()(down0)
  31. # down0 = LeakyReLU(alpha=0.1)(down0)
  32. # down0_pool = MaxPooling2D((2, 2), strides=(2, 2))(down0)
  33. # 128
  34. down1 = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(inputs)
  35. down1 = BatchNormalization()(down1)
  36. down1 = LeakyReLU(alpha=0.1)(down1)
  37. down1 = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(down1)
  38. down1 = BatchNormalization()(down1)
  39. down1 = LeakyReLU(alpha=0.1)(down1)
  40. down1 = Conv2D(16, (1, 1), padding='same', use_bias=use_bias)(down1)
  41. down1 = BatchNormalization()(down1)
  42. down1 = LeakyReLU(alpha=0.1)(down1)
  43. down1 = SeBlock()(down1)
  44. down1_pool = MaxPooling2D((2, 2), strides=(2, 2))(down1)
  45. # 64
  46. down2 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(down1_pool)
  47. down2 = BatchNormalization()(down2)
  48. down2 = LeakyReLU(alpha=0.1)(down2)
  49. down2 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(down2)
  50. down2 = BatchNormalization()(down2)
  51. down2 = LeakyReLU(alpha=0.1)(down2)
  52. down2 = Conv2D(32, (1, 1), padding='same', use_bias=use_bias)(down2)
  53. down2 = BatchNormalization()(down2)
  54. down2 = LeakyReLU(alpha=0.1)(down2)
  55. down2 = SeBlock()(down2)
  56. down2_pool = MaxPooling2D((2, 2), strides=(2, 2))(down2)
  57. # 32
  58. down3 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(down2_pool)
  59. down3 = BatchNormalization()(down3)
  60. down3 = LeakyReLU(alpha=0.1)(down3)
  61. down3 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(down3)
  62. down3 = BatchNormalization()(down3)
  63. down3 = LeakyReLU(alpha=0.1)(down3)
  64. down3 = Conv2D(64, (1, 1), padding='same', use_bias=use_bias)(down3)
  65. down3 = BatchNormalization()(down3)
  66. down3 = LeakyReLU(alpha=0.1)(down3)
  67. down3 = SeBlock()(down3)
  68. down3_pool = MaxPooling2D((2, 2), strides=(2, 2))(down3)
  69. # 16
  70. center = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(down3_pool)
  71. center = BatchNormalization()(center)
  72. center = LeakyReLU(alpha=0.1)(center)
  73. center = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(center)
  74. center = BatchNormalization()(center)
  75. center = LeakyReLU(alpha=0.1)(center)
  76. center = Conv2D(64, (1, 1), padding='same', use_bias=use_bias)(center)
  77. center = BatchNormalization()(center)
  78. center = LeakyReLU(alpha=0.1)(center)
  79. center = SeBlock()(center)
  80. # 32
  81. up3 = UpSampling2D((2, 2))(center)
  82. up3 = concatenate([down3, up3], axis=3)
  83. up3 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(up3)
  84. up3 = BatchNormalization()(up3)
  85. up3 = LeakyReLU(alpha=0.1)(up3)
  86. up3 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(up3)
  87. up3 = BatchNormalization()(up3)
  88. up3 = LeakyReLU(alpha=0.1)(up3)
  89. up3 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(up3)
  90. up3 = BatchNormalization()(up3)
  91. up3 = LeakyReLU(alpha=0.1)(up3)
  92. up3 = Conv2D(64, (1, 1), padding='same', use_bias=use_bias)(up3)
  93. up3 = BatchNormalization()(up3)
  94. up3 = LeakyReLU(alpha=0.1)(up3)
  95. up3 = SeBlock()(up3)
  96. # 64
  97. up2 = UpSampling2D((2, 2))(up3)
  98. up2 = concatenate([down2, up2], axis=3)
  99. up2 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(up2)
  100. up2 = BatchNormalization()(up2)
  101. up2 = LeakyReLU(alpha=0.1)(up2)
  102. up2 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(up2)
  103. up2 = BatchNormalization()(up2)
  104. up2 = LeakyReLU(alpha=0.1)(up2)
  105. up2 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(up2)
  106. up2 = BatchNormalization()(up2)
  107. up2 = LeakyReLU(alpha=0.1)(up2)
  108. up2 = Conv2D(32, (1, 1), padding='same', use_bias=use_bias)(up2)
  109. up2 = BatchNormalization()(up2)
  110. up2 = LeakyReLU(alpha=0.1)(up2)
  111. up2 = SeBlock()(up2)
  112. # 128
  113. up1 = UpSampling2D((2, 2))(up2)
  114. up1 = K.concatenate([down1, up1], axis=3)
  115. up1 = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up1)
  116. up1 = BatchNormalization()(up1)
  117. up1 = LeakyReLU(alpha=0.1)(up1)
  118. up1 = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up1)
  119. up1 = BatchNormalization()(up1)
  120. up1 = LeakyReLU(alpha=0.1)(up1)
  121. up1 = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up1)
  122. up1 = BatchNormalization()(up1)
  123. up1 = LeakyReLU(alpha=0.1)(up1)
  124. up1 = Conv2D(16, (1, 1), padding='same', use_bias=use_bias)(up1)
  125. up1 = BatchNormalization()(up1)
  126. up1 = LeakyReLU(alpha=0.1)(up1)
  127. up1 = SeBlock()(up1)
  128. # # 256
  129. # up0 = UpSampling2D((2, 2))(up1)
  130. # up0 = K.concatenate([down0, up0], axis=3)
  131. # up0 = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up0)
  132. # up0 = BatchNormalization()(up0)
  133. # up0 = LeakyReLU(alpha=0.1)(up0)
  134. # up0 = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up0)
  135. # up0 = BatchNormalization()(up0)
  136. # up0 = LeakyReLU(alpha=0.1)(up0)
  137. # up0 = Conv2D(16, (1, 1), padding='same', use_bias=use_bias)(up0)
  138. # up0 = BatchNormalization()(up0)
  139. # up0 = LeakyReLU(alpha=0.1)(up0)
  140. classify = Conv2D(cls_num, (1, 1), activation='sigmoid')(up1)
  141. # classify = Dense(cls_num, activation="softmax")(up1)
  142. model = Model(inputs=inputs, outputs=classify)
  143. # model.summary(line_length=100)
  144. return model
  145. def seal_model(input_shape, output_shape, cls_num=3):
  146. inputs = Input(shape=input_shape)
  147. use_bias = False
  148. # # 256
  149. # down0 = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(inputs)
  150. # down0 = BatchNormalization()(down0)
  151. # down0 = LeakyReLU(alpha=0.1)(down0)
  152. # down0 = Conv2D(16, (1, 1), padding='same', use_bias=use_bias)(down0)
  153. # down0 = BatchNormalization()(down0)
  154. # down0 = LeakyReLU(alpha=0.1)(down0)
  155. # down0_pool = MaxPooling2D((2, 2), strides=(2, 2))(down0)
  156. # 128
  157. down1 = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(inputs)
  158. down1 = BatchNormalization()(down1)
  159. down1 = LeakyReLU(alpha=0.1)(down1)
  160. down1 = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(down1)
  161. down1 = BatchNormalization()(down1)
  162. down1 = LeakyReLU(alpha=0.1)(down1)
  163. down1 = Conv2D(16, (1, 1), padding='same', use_bias=use_bias)(down1)
  164. down1 = BatchNormalization()(down1)
  165. down1 = LeakyReLU(alpha=0.1)(down1)
  166. down1_pool = MaxPooling2D((2, 2), strides=(2, 2))(down1)
  167. # 64
  168. down2 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(down1_pool)
  169. down2 = BatchNormalization()(down2)
  170. down2 = LeakyReLU(alpha=0.1)(down2)
  171. down2 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(down2)
  172. down2 = BatchNormalization()(down2)
  173. down2 = LeakyReLU(alpha=0.1)(down2)
  174. down2 = Conv2D(32, (1, 1), padding='same', use_bias=use_bias)(down2)
  175. down2 = BatchNormalization()(down2)
  176. down2 = LeakyReLU(alpha=0.1)(down2)
  177. down2_pool = MaxPooling2D((2, 2), strides=(2, 2))(down2)
  178. # 32
  179. down3 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(down2_pool)
  180. down3 = BatchNormalization()(down3)
  181. down3 = LeakyReLU(alpha=0.1)(down3)
  182. down3 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(down3)
  183. down3 = BatchNormalization()(down3)
  184. down3 = LeakyReLU(alpha=0.1)(down3)
  185. down3 = Conv2D(64, (1, 1), padding='same', use_bias=use_bias)(down3)
  186. down3 = BatchNormalization()(down3)
  187. down3 = LeakyReLU(alpha=0.1)(down3)
  188. down3_pool = MaxPooling2D((2, 2), strides=(2, 2))(down3)
  189. # 16
  190. center = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(down3_pool)
  191. center = BatchNormalization()(center)
  192. center = LeakyReLU(alpha=0.1)(center)
  193. center = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(center)
  194. center = BatchNormalization()(center)
  195. center = LeakyReLU(alpha=0.1)(center)
  196. center = Conv2D(64, (1, 1), padding='same', use_bias=use_bias)(center)
  197. center = BatchNormalization()(center)
  198. center = LeakyReLU(alpha=0.1)(center)
  199. # 32
  200. up3 = UpSampling2D((2, 2))(center)
  201. up3 = concatenate([down3, up3], axis=3)
  202. up3 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(up3)
  203. up3 = BatchNormalization()(up3)
  204. up3 = LeakyReLU(alpha=0.1)(up3)
  205. up3 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(up3)
  206. up3 = BatchNormalization()(up3)
  207. up3 = LeakyReLU(alpha=0.1)(up3)
  208. up3 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(up3)
  209. up3 = BatchNormalization()(up3)
  210. up3 = LeakyReLU(alpha=0.1)(up3)
  211. up3 = Conv2D(64, (1, 1), padding='same', use_bias=use_bias)(up3)
  212. up3 = BatchNormalization()(up3)
  213. up3 = LeakyReLU(alpha=0.1)(up3)
  214. # 64
  215. up2 = UpSampling2D((2, 2))(up3)
  216. up2 = concatenate([down2, up2], axis=3)
  217. up2 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(up2)
  218. up2 = BatchNormalization()(up2)
  219. up2 = LeakyReLU(alpha=0.1)(up2)
  220. up2 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(up2)
  221. up2 = BatchNormalization()(up2)
  222. up2 = LeakyReLU(alpha=0.1)(up2)
  223. up2 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(up2)
  224. up2 = BatchNormalization()(up2)
  225. up2 = LeakyReLU(alpha=0.1)(up2)
  226. up2 = Conv2D(32, (1, 1), padding='same', use_bias=use_bias)(up2)
  227. up2 = BatchNormalization()(up2)
  228. up2 = LeakyReLU(alpha=0.1)(up2)
  229. # 128
  230. up1 = UpSampling2D((2, 2))(up2)
  231. up1 = K.concatenate([down1, up1], axis=3)
  232. up1 = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up1)
  233. up1 = BatchNormalization()(up1)
  234. up1 = LeakyReLU(alpha=0.1)(up1)
  235. up1 = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up1)
  236. up1 = BatchNormalization()(up1)
  237. up1 = LeakyReLU(alpha=0.1)(up1)
  238. up1 = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up1)
  239. up1 = BatchNormalization()(up1)
  240. up1 = LeakyReLU(alpha=0.1)(up1)
  241. up1 = Conv2D(16, (1, 1), padding='same', use_bias=use_bias)(up1)
  242. up1 = BatchNormalization()(up1)
  243. up1 = LeakyReLU(alpha=0.1)(up1)
  244. # # 256
  245. # up0 = UpSampling2D((2, 2))(up1)
  246. # up0 = K.concatenate([down0, up0], axis=3)
  247. # up0 = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up0)
  248. # up0 = BatchNormalization()(up0)
  249. # up0 = LeakyReLU(alpha=0.1)(up0)
  250. # up0 = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up0)
  251. # up0 = BatchNormalization()(up0)
  252. # up0 = LeakyReLU(alpha=0.1)(up0)
  253. # up0 = Conv2D(16, (1, 1), padding='same', use_bias=use_bias)(up0)
  254. # up0 = BatchNormalization()(up0)
  255. # up0 = LeakyReLU(alpha=0.1)(up0)
  256. classify = Conv2D(cls_num, (1, 1), activation='sigmoid')(up1)
  257. # classify = Dense(cls_num, activation="softmax")(up1)
  258. model = Model(inputs=inputs, outputs=classify)
  259. model.summary(line_length=100)
  260. return model
  261. def seal_model_small(input_shape, output_shape, cls_num=3):
  262. inputs = Input(shape=input_shape)
  263. use_bias = False
  264. # 128
  265. down1 = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(inputs)
  266. down1 = BatchNormalization()(down1)
  267. down1 = LeakyReLU(alpha=0.1)(down1)
  268. down1 = Conv2D(16, (1, 1), padding='same', use_bias=use_bias)(down1)
  269. down1 = BatchNormalization()(down1)
  270. down1 = LeakyReLU(alpha=0.1)(down1)
  271. down1_pool = MaxPooling2D((2, 2), strides=(2, 2))(down1)
  272. # 64
  273. down2 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(down1_pool)
  274. down2 = BatchNormalization()(down2)
  275. down2 = LeakyReLU(alpha=0.1)(down2)
  276. down2 = Conv2D(32, (1, 1), padding='same', use_bias=use_bias)(down2)
  277. down2 = BatchNormalization()(down2)
  278. down2 = LeakyReLU(alpha=0.1)(down2)
  279. down2_pool = MaxPooling2D((2, 2), strides=(2, 2))(down2)
  280. # 32
  281. down3 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(down2_pool)
  282. down3 = BatchNormalization()(down3)
  283. down3 = LeakyReLU(alpha=0.1)(down3)
  284. down3 = Conv2D(64, (1, 1), padding='same', use_bias=use_bias)(down3)
  285. down3 = BatchNormalization()(down3)
  286. down3 = LeakyReLU(alpha=0.1)(down3)
  287. down3_pool = MaxPooling2D((2, 2), strides=(2, 2))(down3)
  288. # 16
  289. center = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(down3_pool)
  290. center = BatchNormalization()(center)
  291. center = LeakyReLU(alpha=0.1)(center)
  292. center = Conv2D(64, (1, 1), padding='same', use_bias=use_bias)(center)
  293. center = BatchNormalization()(center)
  294. center = LeakyReLU(alpha=0.1)(center)
  295. # 32
  296. up3 = UpSampling2D((2, 2))(center)
  297. up3 = concatenate([down3, up3], axis=3)
  298. up3 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(up3)
  299. up3 = BatchNormalization()(up3)
  300. up3 = LeakyReLU(alpha=0.1)(up3)
  301. up3 = Conv2D(64, (1, 1), padding='same', use_bias=use_bias)(up3)
  302. up3 = BatchNormalization()(up3)
  303. up3 = LeakyReLU(alpha=0.1)(up3)
  304. # 64
  305. up2 = UpSampling2D((2, 2))(up3)
  306. up2 = concatenate([down2, up2], axis=3)
  307. up2 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(up2)
  308. up2 = BatchNormalization()(up2)
  309. up2 = LeakyReLU(alpha=0.1)(up2)
  310. up2 = Conv2D(32, (1, 1), padding='same', use_bias=use_bias)(up2)
  311. up2 = BatchNormalization()(up2)
  312. up2 = LeakyReLU(alpha=0.1)(up2)
  313. # 128
  314. up1 = UpSampling2D((2, 2))(up2)
  315. up1 = K.concatenate([down1, up1], axis=3)
  316. up1 = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up1)
  317. up1 = BatchNormalization()(up1)
  318. up1 = LeakyReLU(alpha=0.1)(up1)
  319. up1 = Conv2D(16, (1, 1), padding='same', use_bias=use_bias)(up1)
  320. up1 = BatchNormalization()(up1)
  321. up1 = LeakyReLU(alpha=0.1)(up1)
  322. classify = Conv2D(cls_num, (1, 1), activation='sigmoid')(up1)
  323. # classify = Dense(cls_num, activation="softmax")(up1)
  324. model = Model(inputs=inputs, outputs=classify)
  325. model.summary(line_length=100)
  326. return model
  327. class SeBlock(Layer):
  328. def __init__(self, reduction=4, **kwargs):
  329. super(SeBlock, self).__init__(**kwargs)
  330. self.reduction = reduction
  331. def build(self, input_shape):
  332. # 构建layer时需要实现
  333. # 手动将该自定义层参数加入,否则参数为0
  334. self.pool = GlobalAveragePooling2D(keepdims=True, name="my_pool")
  335. self.dense_1 = Dense(int(input_shape[-1]) // self.reduction, use_bias=False, activation="relu", name='my_dense_1')
  336. self.dense_2 = Dense(int(input_shape[-1]), use_bias=False, activation="hard_sigmoid", name='my_dense_2')
  337. # self.dense_1.build(input_shape)
  338. # self.dense_2.build((input_shape[0], input_shape[1], input_shape[2], int(input_shape[-1]) // self.reduction))
  339. self._trainable_weights += self.dense_1._trainable_weights
  340. self._trainable_weights += self.dense_2._trainable_weights
  341. super(SeBlock, self).build(input_shape)
  342. def call(self, inputs):
  343. x = self.pool(inputs)
  344. x = self.dense_1(x)
  345. x = self.dense_2(x)
  346. # 给通道加权重
  347. return Multiply()([inputs, x])
  348. VGG_MEAN = [103.939, 116.779, 123.68]
  349. class Vgg16:
  350. def __init__(self, vgg16_npy_path=None):
  351. if vgg16_npy_path is None:
  352. # path = inspect.getfile(Vgg16)
  353. # path = os.path.abspath(os.path.join(path, os.pardir))
  354. # path = os.path.join(path, "vgg16.npy")
  355. # vgg16_npy_path = path
  356. # print(path)
  357. print("there is no vgg_16_npy!")
  358. raise
  359. self.data_dict = np.load(vgg16_npy_path, encoding='latin1', allow_pickle=True).item()
  360. print("npy file loaded")
  361. def build(self, bgr):
  362. """
  363. load variable from npy to build the VGG
  364. :param rgb: rgb image [batch, height, width, 3] values scaled [0, 1]
  365. """
  366. start_time = time.time()
  367. print("build model started")
  368. bgr_scaled = bgr * 255.0
  369. # Convert RGB to BGR
  370. # red, green, blue = tf.split(axis=3, num_or_size_splits=3, value=rgb_scaled)
  371. # print("red", red)
  372. # assert red.get_shape().as_list()[1:] == [224, 224, 1]
  373. # assert green.get_shape().as_list()[1:] == [224, 224, 1]
  374. # assert blue.get_shape().as_list()[1:] == [224, 224, 1]
  375. # bgr = tf.concat(axis=3, values=[
  376. # blue - VGG_MEAN[0],
  377. # green - VGG_MEAN[1],
  378. # red - VGG_MEAN[2],
  379. # ])
  380. # assert bgr.get_shape().as_list()[1:] == [224, 224, 3]
  381. self.conv1_1 = self.conv_layer(bgr_scaled, "conv1_1")
  382. self.conv1_2 = self.conv_layer(self.conv1_1, "conv1_2")
  383. self.pool1 = self.max_pool(self.conv1_2, 'pool1')
  384. self.conv2_1 = self.conv_layer(self.pool1, "conv2_1")
  385. self.conv2_2 = self.conv_layer(self.conv2_1, "conv2_2")
  386. self.pool2 = self.max_pool(self.conv2_2, 'pool2')
  387. self.conv3_1 = self.conv_layer(self.pool2, "conv3_1")
  388. self.conv3_2 = self.conv_layer(self.conv3_1, "conv3_2")
  389. self.conv3_3 = self.conv_layer(self.conv3_2, "conv3_3")
  390. self.pool3 = self.max_pool(self.conv3_3, 'pool3')
  391. self.conv4_1 = self.conv_layer(self.pool3, "conv4_1")
  392. self.conv4_2 = self.conv_layer(self.conv4_1, "conv4_2")
  393. self.conv4_3 = self.conv_layer(self.conv4_2, "conv4_3")
  394. self.pool4 = self.max_pool(self.conv4_3, 'pool4')
  395. self.conv5_1 = self.conv_layer(self.pool4, "conv5_1")
  396. self.conv5_2 = self.conv_layer(self.conv5_1, "conv5_2")
  397. self.conv5_3 = self.conv_layer(self.conv5_2, "conv5_3")
  398. self.pool5 = self.max_pool(self.conv5_3, 'pool5')
  399. # self.fc6 = self.fc_layer(self.pool5, "fc6")
  400. # # assert self.fc6.get_shape().as_list()[1:] == [4096]
  401. # self.relu6 = tf.nn.relu(self.fc6)
  402. #
  403. # self.fc7 = self.fc_layer(self.relu6, "fc7")
  404. # self.relu7 = tf.nn.relu(self.fc7)
  405. #
  406. # self.fc8 = self.fc_layer(self.relu7, "fc8")
  407. #
  408. # self.prob = tf.nn.softmax(self.fc8, name="prob")
  409. # self.data_dict = None
  410. print(("build model finished: %ds" % (time.time() - start_time)))
  411. def avg_pool(self, bottom, name):
  412. return tf.nn.avg_pool(bottom, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)
  413. def max_pool(self, bottom, name):
  414. return tf.nn.max_pool(bottom, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)
  415. def conv_layer(self, bottom, name):
  416. with tf.compat.v1.variable_scope(name):
  417. filt = self.get_conv_filter(name)
  418. conv = tf.nn.conv2d(bottom, filt, [1, 1, 1, 1], padding='SAME')
  419. conv_biases = self.get_bias(name)
  420. bias = tf.nn.bias_add(conv, conv_biases)
  421. relu = tf.nn.relu(bias)
  422. return relu
  423. def fc_layer(self, bottom, name):
  424. with tf.compat.v1.variable_scope(name):
  425. shape = bottom.get_shape().as_list()
  426. dim = 1
  427. for d in shape[1:]:
  428. dim *= d
  429. x = tf.reshape(bottom, [-1, dim])
  430. weights = self.get_fc_weight(name)
  431. biases = self.get_bias(name)
  432. # Fully connected layer. Note that the '+' operation automatically
  433. # broadcasts the biases.
  434. fc = tf.nn.bias_add(tf.matmul(x, weights), biases)
  435. return fc
  436. def get_conv_filter(self, name):
  437. return tf.constant(self.data_dict[name][0], name="filter")
  438. def get_bias(self, name):
  439. return tf.constant(self.data_dict[name][1], name="biases")
  440. def get_fc_weight(self, name):
  441. return tf.constant(self.data_dict[name][0], name="weights")
  442. class Vgg19:
  443. def __init__(self, vgg19_npy_path=None):
  444. if vgg19_npy_path is None:
  445. print("there is no vgg_16_npy!")
  446. raise
  447. self.data_dict = np.load(vgg19_npy_path, encoding='latin1', allow_pickle=True).item()
  448. def build(self, bgr):
  449. """
  450. load variable from npy to build the VGG
  451. :param rgb: rgb image [batch, height, width, 3] values scaled [0, 1]
  452. """
  453. bgr = bgr * 255.0
  454. # bgr = bgr - np.array(VGG_MEAN).reshape((1, 1, 1, 3))
  455. self.conv1_1 = self.conv_layer(bgr, "conv1_1")
  456. self.conv1_2 = self.conv_layer(self.conv1_1, "conv1_2")
  457. self.pool1 = self.max_pool(self.conv1_2, 'pool1')
  458. self.conv2_1 = self.conv_layer(self.pool1, "conv2_1")
  459. self.conv2_2 = self.conv_layer(self.conv2_1, "conv2_2")
  460. self.pool2 = self.max_pool(self.conv2_2, 'pool2')
  461. self.conv3_1 = self.conv_layer(self.pool2, "conv3_1")
  462. self.conv3_2 = self.conv_layer(self.conv3_1, "conv3_2")
  463. self.conv3_3 = self.conv_layer(self.conv3_2, "conv3_3")
  464. self.conv3_4 = self.conv_layer(self.conv3_3, "conv3_4")
  465. self.pool3 = self.max_pool(self.conv3_4, 'pool3')
  466. self.conv4_1 = self.conv_layer(self.pool3, "conv4_1")
  467. self.conv4_2 = self.conv_layer(self.conv4_1, "conv4_2")
  468. self.conv4_3 = self.conv_layer(self.conv4_2, "conv4_3")
  469. self.conv4_4 = self.conv_layer(self.conv4_3, "conv4_4")
  470. self.pool4 = self.max_pool(self.conv4_4, 'pool4')
  471. self.conv5_1 = self.conv_layer(self.pool4, "conv5_1")
  472. self.conv5_2 = self.conv_layer(self.conv5_1, "conv5_2")
  473. self.conv5_3 = self.conv_layer(self.conv5_2, "conv5_3")
  474. self.conv5_4 = self.conv_layer(self.conv5_3, "conv5_4")
  475. self.pool5 = self.max_pool(self.conv5_4, 'pool5')
  476. def avg_pool(self, bottom, name):
  477. return tf.nn.avg_pool(bottom, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)
  478. def max_pool(self, bottom, name):
  479. return tf.nn.max_pool(bottom, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)
  480. def conv_layer(self, bottom, name):
  481. with tf.compat.v1.variable_scope(name):
  482. filt = self.get_conv_filter(name)
  483. conv = tf.nn.conv2d(bottom, filt, [1, 1, 1, 1], padding='SAME')
  484. conv_biases = self.get_bias(name)
  485. bias = tf.nn.bias_add(conv, conv_biases)
  486. relu = tf.nn.relu(bias)
  487. return relu
  488. def fc_layer(self, bottom, name):
  489. with tf.compat.v1.variable_scope(name):
  490. shape = bottom.get_shape().as_list()
  491. dim = 1
  492. for d in shape[1:]:
  493. dim *= d
  494. x = tf.reshape(bottom, [-1, dim])
  495. weights = self.get_fc_weight(name)
  496. biases = self.get_bias(name)
  497. # Fully connected layer. Note that the '+' operation automatically
  498. # broadcasts the biases.
  499. fc = tf.nn.bias_add(tf.matmul(x, weights), biases)
  500. return fc
  501. def get_conv_filter(self, name):
  502. return tf.constant(self.data_dict[name][0], name="filter")
  503. def get_bias(self, name):
  504. return tf.constant(self.data_dict[name][1], name="biases")
  505. def get_fc_weight(self, name):
  506. return tf.constant(self.data_dict[name][0], name="weights")
  507. def tiny_yolo_body(inputs, num_anchors, num_classes):
  508. """Create Tiny YOLO_v3 model CNN body in keras."""
  509. x1 = compose(
  510. DarknetConv2D_BN_Leaky(16, (3, 3), ),
  511. MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
  512. DarknetConv2D_BN_Leaky(32, (3, 3)),
  513. MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
  514. DarknetConv2D_BN_Leaky(64, (3, 3)),
  515. MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
  516. DarknetConv2D_BN_Leaky(128, (3, 3)),
  517. MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
  518. DarknetConv2D_BN_Leaky(256, (3, 3)))(inputs)
  519. x2 = compose(
  520. MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
  521. DarknetConv2D_BN_Leaky(512, (3, 3)),
  522. MaxPooling2D(pool_size=(2, 2), strides=(1, 1), padding='same'),
  523. DarknetConv2D_BN_Leaky(1024, (3, 3)),
  524. DarknetConv2D_BN_Leaky(256, (1, 1)))(x1)
  525. y1 = compose(
  526. DarknetConv2D_BN_Leaky(512, (3, 3)),
  527. DarknetConv2D(num_anchors*(num_classes+5), (1, 1)))(x2)
  528. x2 = compose(
  529. DarknetConv2D_BN_Leaky(128, (1, 1)),
  530. UpSampling2D(2))(x2)
  531. y2 = compose(
  532. Concatenate(),
  533. DarknetConv2D_BN_Leaky(256, (3, 3)),
  534. DarknetConv2D(num_anchors*(num_classes+5), (1, 1)))([x2, x1])
  535. return Model(inputs, [y1, y2])
  536. def tinier_yolo_se_body(inputs, num_anchors, num_classes):
  537. """Create Tiny YOLO_v3 model CNN body in keras."""
  538. x1 = compose(
  539. DarknetConv2D_BN_Leaky(8, (3, 3)),
  540. MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
  541. DarknetConv2D_BN_Leaky(16, (3, 3)),
  542. MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
  543. DarknetConv2D_BN_Leaky(32, (3, 3)),
  544. MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
  545. DarknetConv2D_BN_Leaky(64, (3, 3)),
  546. MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
  547. DarknetConv2D_BN_Leaky(128, (3, 3)),
  548. )(inputs)
  549. x1 = SeBlock()(x1)
  550. x2 = compose(
  551. MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
  552. DarknetConv2D_BN_Leaky(256, (3, 3)),
  553. MaxPooling2D(pool_size=(2, 2), strides=(1, 1), padding='same'),
  554. DarknetConv2D_BN_Leaky(512, (3, 3)),
  555. DarknetConv2D_BN_Leaky(128, (1, 1)),
  556. )(x1)
  557. x2 = SeBlock()(x2)
  558. y1 = compose(
  559. DarknetConv2D_BN_Leaky(256, (3, 3)),
  560. DarknetConv2D(num_anchors*(num_classes+5), (1, 1))
  561. )(x2)
  562. y1 = SeBlock()(y1)
  563. x2 = compose(
  564. DarknetConv2D_BN_Leaky(64, (1, 1)),
  565. UpSampling2D(2)
  566. )(x2)
  567. x2 = SeBlock()(x2)
  568. y2 = compose(
  569. Concatenate(),
  570. DarknetConv2D_BN_Leaky(128, (3, 3)),
  571. DarknetConv2D(num_anchors*(num_classes+5), (1, 1))
  572. )([x2, x1])
  573. y2 = SeBlock()(y2)
  574. model = Model(inputs, [y1, y2])
  575. model.summary(120)
  576. return model
  577. @wraps(Conv2D)
  578. def DarknetConv2D(*args, **kwargs):
  579. """Wrapper to set Darknet parameters for Convolution2D."""
  580. darknet_conv_kwargs = {'kernel_regularizer': l2(5e-4),
  581. 'padding': 'valid' if kwargs.get('strides') == (2, 2) else 'same'}
  582. darknet_conv_kwargs.update(kwargs)
  583. return Conv2D(*args, **darknet_conv_kwargs)
  584. def DarknetConv2D_BN_Leaky(*args, **kwargs):
  585. """Darknet Convolution2D followed by BatchNormalization and LeakyReLU."""
  586. no_bias_kwargs = {'use_bias': False}
  587. no_bias_kwargs.update(kwargs)
  588. return compose(
  589. DarknetConv2D(*args, **no_bias_kwargs),
  590. BatchNormalization(),
  591. LeakyReLU(alpha=0.1))
  592. def get_tiny_inference_model(anchors, num_classes, weights_path='models/tiny_yolo_weights.h5'):
  593. """create the inference model, for Tiny YOLOv3"""
  594. image_input = Input(shape=(None, None, 3))
  595. image_shape = Input(shape=(2,), dtype='int64', name='image_shape')
  596. num_anchors = len(anchors)
  597. model_body = tiny_yolo_body(image_input, num_anchors//2, num_classes)
  598. print('Create Tiny YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))
  599. model_body.load_weights(weights_path)
  600. print('Load weights {}.'.format(weights_path))
  601. boxes, scores, classes = Lambda(yolo_eval,
  602. name='yolo_eval',
  603. arguments={'anchors': anchors,
  604. 'num_classes': num_classes}
  605. )([model_body.output, image_shape])
  606. # boxes, scores, classes = yolo_eval([model_body.output, image_shape], anchors, num_classes)
  607. model = Model([model_body.input, image_shape], [boxes, scores, classes])
  608. # model.summary(120)
  609. return model