Download model/model.py from weryques/pinas: direct link, hf CLI and curl.
- Browser
- Download file 2.62 kB
-
https://huggingface.co/spaces/weryques/pinas/resolve/main/model/model.py
- Command line
-
hf download hf://spaces/weryques/pinas/model/model.py
-
curl -L -o model.py https://huggingface.co/spaces/weryques/pinas/resolve/main/model/model.py
2.62 kB
| from tensorflow.keras import Model | |
| from tensorflow.keras.layers import ( | |
| concatenate, | |
| Dense, | |
| MaxPooling2D, | |
| Flatten, | |
| Activation, | |
| Dropout, | |
| ) | |
| from .soft_attention import SoftAttention | |
| from .inception_v2 import get_inception_resnet_v2_layers | |
| import numpy as np | |
| from io import BytesIO, StringIO | |
| import matplotlib.pyplot as plt | |
| import cv2 | |
| from PIL import Image | |
| def create_model(): | |
| irv2, conv = get_inception_resnet_v2_layers() | |
| attention_layer, _ = SoftAttention( | |
| aggregate=True, | |
| m=16, | |
| concat_with_x=False, | |
| ch=int(conv.shape[-1]), | |
| name="soft_attention", | |
| )(conv) | |
| attention_layer = MaxPooling2D(pool_size=(2, 2), padding="same")(attention_layer) | |
| conv = MaxPooling2D(pool_size=(2, 2), padding="same")(conv) | |
| conv = concatenate([conv, attention_layer]) | |
| conv = Activation("relu")(conv) | |
| conv = Dropout(0.5)(conv) | |
| output = Flatten()(conv) | |
| output = Dense(7, activation="softmax")(output) | |
| model = Model(inputs=irv2.input, outputs=output) | |
| model.load_weights("./data/IRV2+SA.weights.h5") | |
| return model | |
| def get_friendly_output(prediction): | |
| idx = np.argmax(prediction, axis=1)[0] | |
| targetnames = ["akiec", "bcc", "bkl", "df", "mel", "nv", "vasc"] | |
| map_label = { | |
| "akiec": "Ceratoses actínicas e carcinoma intraepitelial / Doença de Bowen", | |
| "bcc": "Carcinoma basocelular", | |
| "bkl": "Lesões benignas semelhantes a ceratose / lentigos solares / ceratose seborreica e ceratose semelhante a líquen plano", | |
| "df": "Dermatofibroma - lesões benignas típicas de extremidades, como braços, antebraços e pernas.", | |
| "mel": "Melanoma - O tipo mais grave de câncer de pele.", | |
| "nv": "Nevos melanocíticos - Distúrbio geralmente não cancerígeno das células da pele produtoras de pigmento, comumente chamado de marcas de nascença ou pintas.", | |
| "vasc": "Lesões vasculares / angiomas / angioqueratomas / granulomas piogênicos e hemorragia", | |
| } | |
| return map_label[targetnames[idx]] | |
| def get_attention_map(image, model): | |
| sa_model = Model(model.inputs, model.get_layer("soft_attention").output) | |
| _, sa_maps = sa_model.predict(image) | |
| sum_attnmap = np.sum(sa_maps[0], 0) | |
| buffer_ = BytesIO() | |
| plt.imshow((image[0] + 1) / 2, alpha=1.0, aspect="auto") | |
| plt.imshow( | |
| cv2.resize(sum_attnmap, (299, 299), interpolation=cv2.INTER_CUBIC), | |
| cmap="jet", | |
| alpha=0.5, | |
| ) | |
| plt.axis('off') | |
| plt.show() | |
| plt.savefig(buffer_, format = "png", bbox_inches='tight', pad_inches=0) | |
| buffer_.seek(0) | |
| return buffer_ | |