import tensorflow as tf
import glob
import cv2
import numpy as np
from skimage.color import rgb2gray
image_file_path = '/drive/My Drive/RoadCracks/Imgs/'
image_file = glob.glob(image_file_path +'*.jpg')
image_file.sort()
X= []
for img in image_file:
image = cv2.imread(img)
image = cv2.resize(image ,(512,512))
image = image.astype('float32')
image = image / np.max(image)
image = rgb2gray(image)
image = image.reshape(512,512,1)
X.append(image)
X = np.array(X)
label_file_path = '/drive/My Drive/RoadCracks/Imgs/'
label_file = glob.glob(label_file_path +'*.png')
label_file.sort()
Y= []
nClasses = 2
seg_labels= np.zeros((151,512, 512, nClasses), dtype='uint8')
for img in label_file:
label = cv2.imread(img)
label = cv2.resize(label ,(512,512))
label = label / np.max(label)
label = label[:,:,0]
for c in range(nClasses):
seg_labels[:,:,:,c] = (label == c)
label = label.astype('uint8')
Y.append(label)
Y = np.array(Y)
Y = Y.reshape(151,512,512,1)
import matplotlib.pyplot as plt
import sys
import cv2
ksize_rows = 64
ksize_cols = 64
# strides_rows and strides_cols determine the distance between
#+ the centers of two consecutive patches.
strides_rows = 32 # 128
strides_cols = 32 # 128
# The size of sliding window
ksizes = [1, ksize_rows, ksize_cols, 1]
# How far the centers of 2 consecutive patches are in the image
strides = [1, strides_rows, strides_cols, 1]
rates = [1, 1, 1, 1] # sample pixel consecutively
padding='VALID' # or 'SAME'
image_patches = []
for i in range(0,151):
sess = tf.InteractiveSession()
im= tf.expand_dims(X[i], 0)
image_patches.append(tf.extract_image_patches(im, ksizes, strides, rates, padding))
seg_patches = []
for i in range(0,151):
sess = tf.InteractiveSession()
im= tf.expand_dims(seg_labels[i], 0)
seg_patches.append(tf.extract_image_patches(im, ksizes, strides, rates, padding))
seg_patches = np.array(seg_patches)
I =[]
L =[]
S =[]
for i in range(0,151):
I.append(tf.reshape(image_patches[i],(225,64,64)))
for j in range(0,151):
L.append(tf.reshape(label_patches[j],(225,64,64)))
for i in range(0,151):
S.append(tf.reshape(seg_patches[i],(225,64,64,2)))
I = tf.reshape(I , (151*225,64,64))
L = tf.reshape(L , (151*225,64,64))
S = tf.reshape(S , (151*225,64,64,2))
total_image_patches_train = tf.reshape(I ,(151*225,64,64,1))[0:30000]
total_label_patches_train = tf.reshape(L ,(151*225,64,64,1))[0:30000]
seg_train = tf.reshape(S , (151*225,64,64,2))[0:30000]
seg_test = tf.reshape(S , (151*225,64,64,2))[30000:]
total_image_patches_test = tf.reshape(I ,(151*225,64,64,1))[30000:]
total_label_patches_test = tf.reshape(L ,(151*225,64,64,1))[30000:]
import tensorflow as tf
from tensorflow import keras
from keras.layers import Input ,Conv2D , DepthwiseConv2D,Conv2DTranspose, MaxPool2D,UpSampling2D , Deconvolution2D , BatchNormalization , Dense , Lambda , Activation
from keras.models import Model
import numpy as np
# functional API
# stage 1
# creating layer
input_imgs =Input(shape =(64,64,1))
# conv1
x= Conv2D(96,(7,7) ,strides= 1, activation ='relu' ,padding ='same' )(input_imgs)
x = BatchNormalization()(x)
x = MaxPool2D((2,2),padding ='valid')(x)
# conv2
x =Conv2D(256 ,(5,5),strides= 1,activation ='relu' , padding ='same')(x)
x = BatchNormalization()(x)
x =MaxPool2D((2,2), padding ='valid')(x)
# conv3
x =Conv2D(384,(3,3),strides= 1, activation ='relu' , padding ='same')(x)
x = BatchNormalization()(x)
# conv4
x = Conv2D(384 , (3,3) ,strides= 1, activation ='relu' , padding ='same')(x)
x = BatchNormalization()(x)
# implement deconvolution part
# # x=Conv2DTranspose(64, (3, 3), strides=(4,4), activation='relu',kernel_initializer='uniform', output_shape=(None, 512, 512, 2))(x)
x = UpSampling2D((4,4) , interpolation = 'bilinear')(x)
# x = Conv2DTranspose(64 ,kernel_size =(4,4), strides =(4,4) , padding ='valid')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
# # we have feature map
# # conv5
y = Conv2D(2 ,(1,1) ,strides=1 , activation ='relu' , padding='same')(x)
y = BatchNormalization()(y)
from keras import optimizers
model.compile(optimizer = optimizers.adam(lr =.001) ,loss = 'binary_crossentropy' , metrics =['accuracy'])
network_history = model.fit(sess.run(total_image_patches_train) , sess.run(seg_train) , batch_size=10 ,epochs =2 ,shuffle =False ,
validation_data=(sess.run(total_image_patches_test) ,sess.run(seg_test)))
model.summary()
Сначала мы читаем изображения и метки с google-диска и создаем метку сегментации (seg_label), затем извлекаем 64 * 64 патча для изображений и меток и seg_label с шагом 32 ...
эта структура сети предназначена для сегментации ...
размер подизображения (64,64), а всего изображения (512,512) ... Я извлек патч изображений и должен проверить на все изображение ...