Advertisement
Danila_lipatov

CNN_model_5l+5BN

Sep 17th, 2024
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1. def createmodel():
  2.     img = layers.Input(shape=imgshape)  # Get image as an input of size 50,200,1
  3.     conv1 = layers.Conv2D(16, (3, 3), padding='same', activation='relu')(img)  # 50*200
  4.     bn1 = layers.BatchNormalization()(conv1)
  5.     # dn1_1 = layers.Dropout(0.2)(bn1)
  6.     mp1 = layers.MaxPooling2D(pool_size=(2, 2),padding='same')(bn1)  # 25*100
  7.     conv2 = layers.Conv2D(32, (3, 3), padding='same', activation='relu')(mp1)
  8.     bn2 = layers.BatchNormalization()(conv2)
  9.     # dn2_2 = layers.Dropout(0.2)(bn2)
  10.     mp2 = layers.MaxPooling2D(pool_size=(2, 2),padding='same')(bn2)  # 13*50
  11.     bn2_2 = layers.BatchNormalization()(mp2)
  12.     conv3 = layers.Conv2D(64, (3, 3), padding='same', activation='relu')(bn2_2)
  13.     bn3 = layers.BatchNormalization()(conv3)  # to improve the stability of model
  14.     # dn3_3 = layers.Dropout(0.2)(bn3)
  15.     mp3 = layers.MaxPooling2D(pool_size=(2, 2),padding='same')(bn3)  # 7*25
  16.     bn3_3 = layers.BatchNormalization()(mp3)  # t
  17.     conv4 = layers.Conv2D(128, (3, 3), padding='same', activation='relu')(bn3_3)
  18.     bn4 = layers.BatchNormalization()(conv4)  # to improve the stability of model
  19.     # dn4_4 = layers.Dropout(0.2)(bn4)
  20.     mp4 = layers.MaxPooling2D(pool_size=(2, 2), padding='same')(bn4)
  21.     # bn4 = layers.BatchNormalization()(mp4)
  22.     bn4_4 = layers.BatchNormalization()(mp4)
  23.     conv5 = layers.Conv2D(256, (3, 3), padding='same', activation='relu')(bn4_4)
  24.     bn5 = layers.BatchNormalization()(conv5)
  25.     mp5 = layers.MaxPooling2D((2, 2), padding='same')(bn5)
  26.     # conv6 = layers.Conv2D(512, (3, 3), padding='same', activation='relu')(mp5)
  27.     # bn6 = layers.BatchNormalization()(conv6)
  28.     # mp6 = layers.MaxPooling2D((2, 2), padding='same')(bn6)
  29.     # final = layers.Dropout(0.3)(mp5)
  30.     # bn5_5 = layers.BatchNormalization()(mp5)
  31.     # conv6 = layers.Conv2D(512, (3, 3), padding='same', activation='relu')(bn5_5)
  32.     # bn6 = layers.BatchNormalization()(conv6)
  33.     # mp6 = layers.MaxPooling2D((2, 2), padding='same')(bn6)
  34.     # bn6_6 = layers.BatchNormalization()(mp6)
  35.     # drop_la = layers.Dropout(0.5)(bn5_5)
  36.     # full = layers.Dropout(0.5)(mp5)
  37.     # bn4 = layers.BatchNormalization()(conv4)  # to improve the stability of model
  38.     # mp4 = layers.MaxPooling2D((2,2), padding='same')(bn4)  # 7*25
  39.  
  40.     flat = layers.Flatten()(mp5)  # convert the layer into 1-D
  41.  
  42.     outs = []
  43.     for _ in range(5):  # for 5 letters of captcha
  44.         dens1 = layers.Dense(512, activation='relu')(flat)
  45.         drop = layers.BatchNormalization()(dens1)
  46.         #1 - sigmoid
  47.         #2 - softmax
  48.         res = layers.Dense(nchar, activation='softmax')(drop)
  49.         # rea_ = layers.Dropout(0.2)(res)  # drops 0.5 fraction of nodes
  50.         # layers.Dropout(0.5)
  51.         outs.append(res)  # result of layers
  52.  
  53.     # Compile model and return it
  54.     # img = img.reshape(1000, 50, 200, 1)
  55.     model = Model(img, outs)  # create model
  56.     # optimizer = keras.optimizers.Adam(learning_rate=0.01)
  57.     model.compile(loss='categorical_crossentropy', optimizer='Adam', metrics=["accuracy"]*5)
  58.     return model
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement