基于計算機視覺的裂紋檢測方案

極市導(dǎo)讀
?本篇文章中,作者為異常識別和定位提供了一種機器學(xué)習(xí)解決方案。所有這些功能都可以通過實現(xiàn)單個分類模型來訪問。在訓(xùn)練過程中,該神經(jīng)網(wǎng)絡(luò)會獲取所有相關(guān)信息,從而可以進行分類,并在最后給出墻壁裂紋的信息。?>>加入極市CV技術(shù)交流群,走在計算機視覺的最前沿
01. 數(shù)據(jù)集
images = []for url in tqdm.tqdm(df['content']):response = requests.get(url)img = Image.open(BytesIO(response.content))img = img.resize((224, 224))numpy_img = img_to_array(img)img_batch = np.expand_dims(numpy_img, axis=0)images.append(img_batch.astype('float16'))images = np.vstack(images)

02. 機器學(xué)習(xí)模型
vgg_conv = vgg16.VGG16(weights='imagenet', include_top=False, input_shape = (224, 224, 3))for layer in vgg_conv.layers[:-8]:layer.trainable = False
x = vgg_conv.outputx = GlobalAveragePooling2D()(x)x = Dense(2, activation="softmax")(x)model = Model(vgg_conv.input, x)model.compile(loss = "categorical_crossentropy", optimizer = optimizers.SGD(lr=0.0001, momentum=0.9), metrics=["accuracy"])

def plot_activation(img):pred = model.predict(img[np.newaxis,:,:,:])pred_class = np.argmax(pred)weights = model.layers[-1].get_weights()[0]class_weights = weights[:, pred_class]intermediate = Model(model.input,model.get_layer("block5_conv3").output)conv_output = intermediate.predict(img[np.newaxis,:,:,:])conv_output = np.squeeze(conv_output)h = int(img.shape[0]/conv_output.shape[0])w = int(img.shape[1]/conv_output.shape[1])act_maps = sp.ndimage.zoom(conv_output, (h, w, 1), order=1)out = np.dot(act_maps.reshape((img.shape[0]*img.shape[1],512)),class_weights).reshape(img.shape[0],img.shape[1])plt.imshow(img.astype('float32').reshape(img.shape[0],img.shape[1],3))plt.imshow(out, cmap='jet', alpha=0.35)plt.title('Crack' if pred_class == 1 else 'No Crack')

03. 總結(jié)
推薦閱讀

評論
圖片
表情
