VGG16의 특징 맵 시각화
14445 단어 TensorFlow파이썬딥러닝기계 학습
VGG16이란?
컨볼루션 13층과 풀 결합 3층의 총 16층으로 이루어진 컨벌루션 신경망을 말한다.
2014년의 ILSVRC(ImageNet Large Scale Visual Recognition Challenge)라는 이미지 분류의 대회에서 제안되었다.
구성은 아래 그림을 참조하십시오.

시각화하다
실제로 특징 맵을 가시화해 간다.
실제로 특징 맵을 가시화해 간다.
을 준비하는 것만으로 가시화할 수 있다. 학습이 끝난 모델에 관해서는 import하면 자동으로 다운로드해 준다. 편리.
특징 맵 선택
의 2 종류를 가시화한다.
입력에 사용되는 이미지

이번에는 덩굴의 이미지를 사용합니다. 라고 하는 것도 조계의 화상은 특징 맵이 비교적 보기 쉽기 때문이다.
소스 코드
이미지 로드
# NOTE: Load image.
img = image.load_img(args.img, target_size=(224, 224))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
print("IMAGE FILENAME: %s" % os.path.basename(args.img))
학습된 모델 로드
# NOTE: Load trained model.
print("[status] Loaded model...")
if args.model is not None:
model = load_model(args.model)
elif not (args.model_json is None or args.model_weight is None):
model = model_from_json(open(args.model_json).read())
model.load_weights(args.model_weight)
else:
model = VGG16(weights="imagenet")
출력할 레이어 선택(Convolution and Pooling Layer)
# NOTE: Select output layer and predict.
print("[status] Extract input image features...")
layers = model.layers[1:19]
layer_outputs = [layer.output for layer in layers]
activation_model = models.Model(inputs=model.input, outputs=layer_outputs)
# activation_model.summary()
activations = activation_model.predict(img)
# NOTE: Select visualize layers.
conv_and_pool_activations = []
for layer, activation in zip(layers, activations):
is_pooling_layer = isinstance(layer, MaxPooling2D)
is_convolution_layer = isinstance(layer, Convolution2D)
if is_pooling_layer or is_convolution_layer:
conv_and_pool_activations.append([layer.name, activation])
특징 맵을 히트 맵으로 변환하고 저장
# NOTE: Generate heatmap.
print("[status] Generating heatmaps...")
os.makedirs(args.directory, exist_ok=True)
for i, (name, activation) in enumerate(conv_and_pool_activations):
print("[status] Processing %s layer..." % name)
n_imgs = activation.shape[3]
n_cols = math.ceil(math.sqrt(n_imgs))
n_rows = math.floor(n_imgs / n_cols)
screens = []
for y in range(0, n_rows):
rows = []
for x in range(0, n_cols):
j = y * n_cols + x
if j < n_imgs:
featuremap_img = activation[0, :, :, j]
rows.append(featuremap_img)
else:
rows.append(np.zeros())
screens.append(np.concatenate(rows, axis=1))
screens = np.concatenate(screens, axis=0)
plt.figure()
sns.heatmap(screens, xticklabels=False, yticklabels=False)
save_name = "%s.png" % name
save_path = os.path.join(args.directory, save_name)
plt.savefig(save_path)
plt.close()
print("[status] Generating heatmap has finished...")
출력 결과

Layer가 진행됨에 따라 이미지는 작아지고, 특징도 대략적으로 되어 있다. 이것만으로는 이마이치 모르기 때문에, 특징 강도 중에서 평균 강도가 가장 높은 특징 강도를 선택해 보았다.

마지막은 대략적인 특징을 포착하고 있다. 대단해.
끝에
VGG16의 특징 맵을 시각화했습니다.
이번에 사용한 코드는 GitHub 에 올리고 있습니다.
https://魏Tub. 소 m/유즈키 타카13-1110
Reference
이 문제에 관하여(VGG16의 특징 맵 시각화), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/yukitaka13-1110/items/f4d4b38a24ca07045791
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여(VGG16의 특징 맵 시각화), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/yukitaka13-1110/items/f4d4b38a24ca07045791텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)