基于计算机视觉的裂纹检测方案
极市导读
本篇文章中,作者为异常识别和定位提供了一种机器学习解决方案。所有这些功能都可以通过实现单个分类模型来访问。在训练过程中,该神经网络会获取所有相关信息,从而可以进行分类,并在最后给出墙壁裂纹的信息。 >>加入极市CV技术交流群,走在计算机视觉的最前沿
01. 数据集
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. 机器学习模型
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.output
x = 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. 总结
推荐阅读
评论