Initial commit.
Browse filesFiles changed (16)
- .gitattributes+3
- README.mdAdded+20
- binary_mask.jpgAdded
- config.jsonAdded+7
- hircin_the_cat.pngAdded
- image/.DS_StoreAdded
- image/binary_mask.jpgAdded
- image/mask.jpgAdded
- image/raw_output.jpgAdded
- keras_metadata.pbAdded+3
- mask.jpgAdded
- pipeline.pyAdded+79
- raw_output.jpgAdded
- tf_model.h5Added+3
- variables/variables.data-00000-of-00001Added+3
- variables/variables.indexAdded
24 unmodified lines25262724 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zstandard filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text24 unmodified lines25262728293024 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zstandard filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -textkeras_metadata.pb filter=lfs diff=lfs merge=lfs -textvariables/ filter=lfs diff=lfs merge=lfs -textvariables/variables.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
1234567891011121314151617181920---tags:- image-segmentation- genericlibrary_name: genericdataset:- oxfort-iit petslicense: apache-2.0---## Keras semantic segmentation models on the 🤗Hub! 🐶 🐕 🐩
Image classification task tells us about a class assigned to an image, and object detection task creates a boundary box on an object in an image. But what if we want to know about the shape of the image? Segmentation models helps us segment images and reveal their shapes. It has many variants. You can host your Keras segmentation models on the Hub.Semantic segmentation models classify pixels, meaning, they assign a class (can be cat or dog) to each pixel. The output of a model looks like following.We need to get the best prediction for every pixel.This is still not readable. We have to convert this into different binary masks for each class and convert to a readable format by converting each mask into base64. We will return a list of dicts, and for each dictionary, we have the label itself, the base64 code and a score (semantic segmentation models don't return a score, so we have to return 1.0 for this case). You can find the full implementation in ```pipeline.py```.Now that you know the expected output by the model, you can host your Keras segmentation models (and other semantic segmentation models) in the similar fashion. Try it yourself and host your segmentation models!1234567{ "id2label": { "0": 0, "1": 1, "2": 2 }}123version https://git-lfs.github.com/spec/v1oid sha256:e7844f7cdd94bacbd2dceb2000b172c07e3a3db345cd29ea0d51d66107ff28e9size 56303312345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879import jsonfrom typing import Any, Dict, List
import tensorflow as tffrom tensorflow import kerasimport base64import ioimport osimport numpy as npfrom PIL import Image
class PreTrainedPipeline(): def __init__(self, path: str): # load the model self.model = keras.models.load_model(os.path.join(path, "tf_model.h5"))
def __call__(self, inputs: "Image.Image")-> List[Dict[str, Any]]:
# convert img to numpy array, resize and normalize to make the prediction #with Image.open(inputs) as img: img = np.array(inputs)
im = tf.image.resize(img, (128, 128)) im = tf.cast(im, tf.float32) / 255.0 pred_mask = model.predict(im[tf.newaxis, ...]) # take the best performing class for each pixel # the output of argmax looks like this [[1, 2, 0], ...] pred_mask_arg = tf.argmax(pred_mask, axis=-1)
labels = [] # convert the prediction mask into binary masks for each class binary_masks = {} mask_codes = {} # when we take tf.argmax() over pred_mask, it becomes a tensor object # the shape becomes TensorShape object, looking like this TensorShape([128]) # we need to take get shape, convert to list and take the best one rows = pred_mask_arg[0][1].get_shape().as_list()[0] cols = pred_mask_arg[0][2].get_shape().as_list()[0] for cls in range(pred_mask.shape[-1]):
binary_masks[f"mask_{cls}"] = np.zeros(shape = (pred_mask.shape[1], pred_mask.shape[2])) #create masks for each class for row in range(rows):
for col in range(cols):
if pred_mask_arg[0][row][col] == cls: binary_masks[f"mask_{cls}"][row][col] = 1 else: binary_masks[f"mask_{cls}"][row][col] = 0
mask = binary_masks[f"mask_{cls}"] mask *= 255 img = Image.fromarray(mask.astype(np.int8), mode="L") # we need to make it readable for the widget with io.BytesIO() as out: img.save(out, format="PNG") png_string = out.getvalue() mask = base64.b64encode(png_string).decode("utf-8")
mask_codes[f"mask_{cls}"] = mask
# widget needs the below format, for each class we return label and mask string labels.append({ "label": f"LABEL_{cls}", "mask": mask_codes[f"mask_{cls}"], "score": 1.0, }) return labels123version https://git-lfs.github.com/spec/v1oid sha256:0258ea75c11d977fae78f747902e48541c5e6996d3d5c700175454ffeb42aa0fsize 63661584123version https://git-lfs.github.com/spec/v1oid sha256:e3c27336aaecafd070749d833780e6603b9c25caf01a037c0ef9a93ff3b0c36csize 63405929.gitattributes+3
README.mdAdded+20
binary_mask.jpgAdded

config.jsonAdded+7
hircin_the_cat.pngAdded

image/.DS_StoreAdded
image/binary_mask.jpgAdded

image/mask.jpgAdded

image/raw_output.jpgAdded

keras_metadata.pbAdded+3
mask.jpgAdded

pipeline.pyAdded+79
raw_output.jpgAdded

tf_model.h5Added+3
variables/variables.data-00000-of-00001Added+3
variables/variables.indexAdded