Last active
June 14, 2020 22:02
-
-
Save fredgido/700398dcb24cdcfbe55326990bc0dca8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tensorflow as tf | |
| from typing import Any, Union | |
| import six | |
| import math | |
| import numpy as np | |
| from skimage.transform import AffineTransform | |
| from skimage import transform | |
| import pprint | |
| model = tf.keras.models.load_model("C:\\deepdanbooru-v3-20200101-sgd-e30\\model-resnet_custom_v3.h5") | |
| def load_image_for_evaluate(input_: Union[str, six.BytesIO], width: int, height: int, normalize: bool = True) -> Any: | |
| if isinstance(input_, six.BytesIO): | |
| image_raw = input_.getvalue() | |
| else: | |
| image_raw = tf.io.read_file(input_) | |
| image = tf.io.decode_png(image_raw, channels=3) | |
| image = tf.image.resize(image, size=(height, width), method=tf.image.ResizeMethod.AREA, preserve_aspect_ratio=True) | |
| image = image.numpy() # EagerTensor to np.array | |
| image = transform.warp(image, (AffineTransform(translation=(-image.shape[1] * 0.5, -image.shape[0] * 0.5)) +AffineTransform(translation=(width * 0.5, height * 0.5))).inverse, output_shape=(width,height), order=1, mode='edge') | |
| if normalize: | |
| image = image / 255.0 | |
| return image | |
| url = "https://danbooru.donmai.us/data/sample/sample-3caa27ff7617fa2691e0727ef9104240.jpg" | |
| imagefile = tf.keras.utils.get_file( url.split("/")[-1], url) # imagefile could be a path | |
| r = load_image_for_evaluate(imagefile,512,512) | |
| image = r.reshape((1, r.shape[0], r.shape[1], r.shape[2])) | |
| y = model.predict(image)[0] | |
| imagenet_labels = open("C:\\deepdanbooru-v3-20200101-sgd-e30\\tags.txt").read().splitlines() | |
| rez = zip(list(y.tolist()), list(range(0, len(y)-1))) | |
| t= sorted(rez, key=lambda x: x[0], reverse=True) | |
| cor = [(a, imagenet_labels[b]) for a, b in t] | |
| pprint.pprint(cor[:20]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment