1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| def unet_model(input_shape): inputs = layers.Input(shape=input_shape)
c1 = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(inputs) c1 = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(c1) p1 = layers.MaxPooling2D((2, 2))(c1)
c2 = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(p1) c2 = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(c2) p2 = layers.MaxPooling2D((2, 2))(c2)
c3 = layers.Conv2D(256, (3, 3), activation='relu', padding='same')(p2) c3 = layers.Conv2D(256, (3, 3), activation='relu', padding='same')(c3) p3 = layers.MaxPooling2D((2, 2))(c3)
c4 = layers.Conv2D(512, (3, 3), activation='relu', padding='same')(p3) c4 = layers.Conv2D(512, (3, 3), activation='relu', padding='same')(c4)
u5 = layers.Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(c4) u5 = layers.concatenate([u5, c3]) c5 = layers.Conv2D(256, (3, 3), activation='relu', padding='same')(u5) c5 = layers.Conv2D(256, (3, 3), activation='relu', padding='same')(c5)
u6 = layers.Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(c5) u6 = layers.concatenate([u6, c2]) c6 = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(u6) c6 = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(c6)
u7 = layers.Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(c6) u7 = layers.concatenate([u7, c1]) c7 = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(u7) c7 = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(c7)
outputs = layers.Conv2D(1, (1, 1), activation='sigmoid')(c7)
model = models.Model(inputs=[inputs], outputs=[outputs]) return model
model = unet_model((128, 128, 3)) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
|