{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport json\nimport os\nimport random\nimport matplotlib.pyplot as plt\n#from tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport tensorflow as tf\nfrom tensorflow.keras import layers \nfrom sklearn.preprocessing import LabelEncoder\nfrom tensorflow import keras\nBATCH=128","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-02T13:11:08.347561Z","iopub.execute_input":"2022-08-02T13:11:08.348241Z","iopub.status.idle":"2022-08-02T13:11:14.639581Z","shell.execute_reply.started":"2022-08-02T13:11:08.348205Z","shell.execute_reply":"2022-08-02T13:11:14.638443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_DIR = \"../input/herbarium-2022-fgvc9/train_images/\"\nTEST_DIR = \"../input/herbarium-2022-fgvc9/test_images/\"\n\nwith open(\"../input/herbarium-2022-fgvc9/train_metadata.json\") as json_file:\n    train_meta = json.load(json_file)\nwith open(\"../input/herbarium-2022-fgvc9/test_metadata.json\") as json_file:\n    test_meta = json.load(json_file)\n#Create a meta-data df that can be used to call in images\nids = []\ncategories = []\npaths = []\n\nfor annotation, image in zip(train_meta['annotations'], train_meta['images']):\n    ids.append(image[\"image_id\"])\n    categories.append(annotation['category_id'])\n    paths.append(image[\"file_name\"])\n\ndf_meta = pd.DataFrame({\"id\":ids, \"category\":categories, \"path\":paths})\ndf_meta.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:14.641595Z","iopub.execute_input":"2022-08-02T13:11:14.642251Z","iopub.status.idle":"2022-08-02T13:11:29.496479Z","shell.execute_reply.started":"2022-08-02T13:11:14.64221Z","shell.execute_reply":"2022-08-02T13:11:29.495046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sci_name = {cat[\"category_id\"]:cat[\"scientificName\"] for cat in train_meta['categories']}\nfamily = {cat[\"category_id\"]:cat[\"family\"] for cat in train_meta['categories']}\ngenus = {cat[\"category_id\"]:cat[\"genus\"] for cat in train_meta['categories']}\nspecies = {cat[\"category_id\"]:cat[\"species\"] for cat in train_meta['categories']}\n\ndf_meta[\"scientific_name\"] = df_meta[\"category\"].map(sci_name)\ndf_meta[\"family\"] = df_meta[\"category\"].map(family)\ndf_meta[\"genus\"] = df_meta[\"category\"].map(genus)\ndf_meta[\"species\"] = df_meta[\"category\"].map(species)\ndf_meta.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:29.498245Z","iopub.execute_input":"2022-08-02T13:11:29.498637Z","iopub.status.idle":"2022-08-02T13:11:29.664304Z","shell.execute_reply.started":"2022-08-02T13:11:29.498599Z","shell.execute_reply":"2022-08-02T13:11:29.663286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_image(index):\n    path=os.path.join(\"../input/herbarium-2022-fgvc9/train_images\",df_meta[\"path\"][index])\n    print(df_meta[\"scientific_name\"][index])\n    plt.imshow(plt.imread(path)/255)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:29.666907Z","iopub.execute_input":"2022-08-02T13:11:29.667932Z","iopub.status.idle":"2022-08-02T13:11:29.673672Z","shell.execute_reply.started":"2022-08-02T13:11:29.667891Z","shell.execute_reply":"2022-08-02T13:11:29.672686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_image(20)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:29.675014Z","iopub.execute_input":"2022-08-02T13:11:29.675955Z","iopub.status.idle":"2022-08-02T13:11:30.084249Z","shell.execute_reply.started":"2022-08-02T13:11:29.675919Z","shell.execute_reply":"2022-08-02T13:11:30.083257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LABEL=len(train_meta['categories'])\nLABEL","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:30.08524Z","iopub.execute_input":"2022-08-02T13:11:30.085588Z","iopub.status.idle":"2022-08-02T13:11:30.092908Z","shell.execute_reply.started":"2022-08-02T13:11:30.085555Z","shell.execute_reply":"2022-08-02T13:11:30.09186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta['categories'][1]","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:30.094733Z","iopub.execute_input":"2022-08-02T13:11:30.095173Z","iopub.status.idle":"2022-08-02T13:11:30.103903Z","shell.execute_reply.started":"2022-08-02T13:11:30.095138Z","shell.execute_reply":"2022-08-02T13:11:30.10283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"family_id=LabelEncoder()\ngenus_id=LabelEncoder()\nfamily_id.fit([x [\"family\"] for x in train_meta[\"categories\"]])\ngenus_id.fit([x [\"genus\"] for x in train_meta[\"categories\"]])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:30.105755Z","iopub.execute_input":"2022-08-02T13:11:30.106638Z","iopub.status.idle":"2022-08-02T13:11:30.133272Z","shell.execute_reply.started":"2022-08-02T13:11:30.106597Z","shell.execute_reply":"2022-08-02T13:11:30.132239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#adding sublabels to make this job easier\ndata_df=df_meta.drop(columns=[\"species\",\"scientific_name\"])\ndata_df[\"family\"]=family_id.transform(data_df[\"family\"])\ndata_df[\"genus\"]=genus_id.transform(data_df[\"genus\"])\ndata_df","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:30.13617Z","iopub.execute_input":"2022-08-02T13:11:30.136452Z","iopub.status.idle":"2022-08-02T13:11:30.802414Z","shell.execute_reply.started":"2022-08-02T13:11:30.136427Z","shell.execute_reply":"2022-08-02T13:11:30.801476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"family_id.inverse_transform([195]),genus_id.inverse_transform([0])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:30.806499Z","iopub.execute_input":"2022-08-02T13:11:30.807573Z","iopub.status.idle":"2022-08-02T13:11:30.818871Z","shell.execute_reply.started":"2022-08-02T13:11:30.807532Z","shell.execute_reply":"2022-08-02T13:11:30.817555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_numbers(index,data=data_df,prefix=\"../input/herbarium-2022-fgvc9/train_images\"):\n    path=os.path.join(prefix,data[\"path\"][index])\n    cat=data[\"category\"][index]\n    family=data[\"family\"][index]\n    genus=data[\"genus\"][index]\n    x=tf.keras.preprocessing.image.load_img(path,target_size=(256,256))\n    x=tf.keras.utils.img_to_array(x)\n    \n    return({\"full_name\":tf.constant(cat,tf.int64),\n            \"family\":tf.constant(family,tf.int64),\n            \"genus\":tf.constant(genus,tf.int64),\n            \"x\":tf.constant(x)})\nb=get_numbers(3)\nb.items()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:30.820455Z","iopub.execute_input":"2022-08-02T13:11:30.821416Z","iopub.status.idle":"2022-08-02T13:11:33.517617Z","shell.execute_reply.started":"2022-08-02T13:11:30.821371Z","shell.execute_reply":"2022-08-02T13:11:33.516351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"specs={k:tf.TensorSpec.from_tensor(v) for k,v in b.items()}\nspecs","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:33.519284Z","iopub.execute_input":"2022-08-02T13:11:33.519985Z","iopub.status.idle":"2022-08-02T13:11:33.528861Z","shell.execute_reply.started":"2022-08-02T13:11:33.519942Z","shell.execute_reply":"2022-08-02T13:11:33.52757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_test_numbers(index,data=data_df,prefix=\"../input/herbarium-2022-fgvc9/train_images\"):\n    path=os.path.join(prefix,data[\"path\"][index])\n    cat=data[\"category\"][index]\n    x=tf.keras.preprocessing.image.load_img(path,target_size=(256,256))\n    x=tf.keras.utils.img_to_array(x)\n    \n    return {\"x\":tf.constant(x),\"y\":tf.constant(cat,tf.int64)}\n    \nb=get_test_numbers(3)\nb","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:33.530798Z","iopub.execute_input":"2022-08-02T13:11:33.531192Z","iopub.status.idle":"2022-08-02T13:11:33.559423Z","shell.execute_reply.started":"2022-08-02T13:11:33.531153Z","shell.execute_reply":"2022-08-02T13:11:33.558555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_specs={k:tf.TensorSpec.from_tensor(v) for k,v in b.items()}\ntest_specs","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:33.560952Z","iopub.execute_input":"2022-08-02T13:11:33.561476Z","iopub.status.idle":"2022-08-02T13:11:33.574385Z","shell.execute_reply.started":"2022-08-02T13:11:33.56144Z","shell.execute_reply":"2022-08-02T13:11:33.571854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class generator():\n    def __init__(self,data):\n        self.cap=len(data)\n        self.data=data \n    def generate(self):\n        for index in range(self.cap):\n            yield get_numbers(index,self.data)\nclass test_generator():\n    def __init__(self,data):\n        self.cap=len(data)\n        self.data=data \n    def generate(self):\n        for index in range(self.cap):\n            yield get_test_numbers(index,self.data)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:33.57618Z","iopub.execute_input":"2022-08-02T13:11:33.576452Z","iopub.status.idle":"2022-08-02T13:11:33.584538Z","shell.execute_reply.started":"2022-08-02T13:11:33.576428Z","shell.execute_reply":"2022-08-02T13:11:33.583167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=data_df.sample(frac=0.8)\ntrain=train.set_index(np.arange(len(train)))\nval=data_df.drop(train.index)\nval=val.set_index(np.arange(len(val)))\ntrain.iloc[0:3]","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:33.586104Z","iopub.execute_input":"2022-08-02T13:11:33.586956Z","iopub.status.idle":"2022-08-02T13:11:33.979509Z","shell.execute_reply.started":"2022-08-02T13:11:33.586905Z","shell.execute_reply":"2022-08-02T13:11:33.978469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train[\"id\"])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:33.981048Z","iopub.execute_input":"2022-08-02T13:11:33.981655Z","iopub.status.idle":"2022-08-02T13:11:33.989444Z","shell.execute_reply.started":"2022-08-02T13:11:33.98162Z","shell.execute_reply":"2022-08-02T13:11:33.988337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a=generator(train) \nb=next(a.generate())\n{k:v.shape for k,v in b.items()}","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:33.991187Z","iopub.execute_input":"2022-08-02T13:11:33.991687Z","iopub.status.idle":"2022-08-02T13:11:34.046412Z","shell.execute_reply.started":"2022-08-02T13:11:33.991647Z","shell.execute_reply":"2022-08-02T13:11:34.045406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FAM=len(family_id.classes_)\nGEN=len(genus_id.classes_)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:34.048094Z","iopub.execute_input":"2022-08-02T13:11:34.048453Z","iopub.status.idle":"2022-08-02T13:11:34.053568Z","shell.execute_reply.started":"2022-08-02T13:11:34.04841Z","shell.execute_reply":"2022-08-02T13:11:34.052293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset=tf.data.Dataset.from_generator(generator(train).generate,output_signature=specs)\ntrain_dataset=train_dataset.batch(BATCH).prefetch(tf.data.AUTOTUNE) \nb=next(train_dataset.as_numpy_iterator())\nprint({k:v.shape for k,v in b.items()})\n\n\nval_dataset=tf.data.Dataset.from_generator(test_generator(val).generate,output_signature=test_specs)\nval_dataset=val_dataset.batch(BATCH)\nb=next(val_dataset.as_numpy_iterator())\nprint({k:v.shape for k,v in b.items()})","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:34.055626Z","iopub.execute_input":"2022-08-02T13:11:34.056034Z","iopub.status.idle":"2022-08-02T13:11:40.564153Z","shell.execute_reply.started":"2022-08-02T13:11:34.055998Z","shell.execute_reply":"2022-08-02T13:11:40.563078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_x=val_dataset.map(lambda x: x[\"x\"])\nval_y=val_dataset.map(lambda x: x[\"y\"])\n\nb=next(val_x.as_numpy_iterator())\nprint(b.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:40.565572Z","iopub.execute_input":"2022-08-02T13:11:40.566278Z","iopub.status.idle":"2022-08-02T13:11:42.25732Z","shell.execute_reply.started":"2022-08-02T13:11:40.566238Z","shell.execute_reply":"2022-08-02T13:11:42.256192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_dataset=tf.data.Dataset.zip((val_x,val_y)).prefetch(tf.data.AUTOTUNE)\nb=next(val_dataset.as_numpy_iterator())\nprint([x.shape for x in b])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:42.259066Z","iopub.execute_input":"2022-08-02T13:11:42.259435Z","iopub.status.idle":"2022-08-02T13:11:46.645926Z","shell.execute_reply.started":"2022-08-02T13:11:42.259399Z","shell.execute_reply":"2022-08-02T13:11:46.644837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(specs)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:46.64981Z","iopub.execute_input":"2022-08-02T13:11:46.651754Z","iopub.status.idle":"2022-08-02T13:11:46.659504Z","shell.execute_reply.started":"2022-08-02T13:11:46.651723Z","shell.execute_reply":"2022-08-02T13:11:46.658357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"OLD_LABEL=LABEL","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:46.661051Z","iopub.execute_input":"2022-08-02T13:11:46.661401Z","iopub.status.idle":"2022-08-02T13:11:46.679001Z","shell.execute_reply.started":"2022-08-02T13:11:46.661367Z","shell.execute_reply":"2022-08-02T13:11:46.677868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size=len(train)//BATCH \nval_size=len(val)//BATCH\n\ntrain_size,val_size","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:46.692148Z","iopub.execute_input":"2022-08-02T13:11:46.692694Z","iopub.status.idle":"2022-08-02T13:11:46.699763Z","shell.execute_reply.started":"2022-08-02T13:11:46.692667Z","shell.execute_reply":"2022-08-02T13:11:46.698387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.config.list_logical_devices()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:47.197395Z","iopub.execute_input":"2022-08-02T13:11:47.198133Z","iopub.status.idle":"2022-08-02T13:11:47.206894Z","shell.execute_reply.started":"2022-08-02T13:11:47.198094Z","shell.execute_reply":"2022-08-02T13:11:47.205651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.cardinality(),val_dataset.cardinality()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:47.529927Z","iopub.execute_input":"2022-08-02T13:11:47.530895Z","iopub.status.idle":"2022-08-02T13:11:47.540171Z","shell.execute_reply.started":"2022-08-02T13:11:47.53085Z","shell.execute_reply":"2022-08-02T13:11:47.538726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs=layers.Input([256,256,3]) \n\nx=layers.Rescaling(1/255)(inputs)\nx=layers.GaussianNoise(0.05)(x)\nx=layers.Conv2D(32,3,activation=\"relu\")(x)\nx=layers.Conv2D(32,3,activation=\"relu\")(x)\nx=layers.MaxPool2D()(x)\n\nx=layers.Conv2D(64,3,activation=\"relu\")(x)\nx=layers.MaxPool2D()(x)\n\nx=layers.Conv2D(64,3,activation=\"relu\")(x)\nx=layers.MaxPool2D()(x)\n\nx=layers.Conv2D(64,3,activation=\"relu\")(x)\nx=layers.MaxPool2D()(x)\n\nx=layers.Conv2D(64,3,activation=\"relu\")(x)\nx=layers.MaxPool2D()(x)\n\nx=layers.Flatten()(x) \nx=layers.Dropout(0.2)(x)\nx=layers.Dense(1024 ,activation=\"gelu\")(x)\nlatent=keras.Model(inputs,x) \n\nlatent.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:48.224278Z","iopub.execute_input":"2022-08-02T13:11:48.225007Z","iopub.status.idle":"2022-08-02T13:11:48.348604Z","shell.execute_reply.started":"2022-08-02T13:11:48.224963Z","shell.execute_reply":"2022-08-02T13:11:48.347483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model_1=Classifier(latent,metrics={\"accuracy\":\"SparseCategoricalAccuracy\"})\n#model_1.compile(optimizer=\"adam\",loss=\"sparse_categorical_crossentropy\")\n#history=model_1.fit( train_dataset,epochs=10,steps_per_epoch=1,validation_data=val_dataset,validation_steps=2)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:49.146849Z","iopub.execute_input":"2022-08-02T13:11:49.147835Z","iopub.status.idle":"2022-08-02T13:11:49.152417Z","shell.execute_reply.started":"2022-08-02T13:11:49.147799Z","shell.execute_reply":"2022-08-02T13:11:49.151479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mini=train_meta[\"categories\"]\nmini=[{\"category\":a[\"category_id\"],\"family\":family_id.transform([a[\"family\"]])[0],\"genus\":genus_id.transform([a[\"genus\"]])[0]} for a in mini] \ngenus_family={}\ncategory_genus=[]\nfor a in mini:\n    genus_family.update({a[\"genus\"]:a[\"family\"]})\n    category_genus.append([a[\"category\"],a[\"genus\"]]) \ngenus_family=[[k,v] for k,v in genus_family.items()]\ngenus_family=tf.sparse.SparseTensor(genus_family,[1 for _ in range(len(genus_family))],(GEN,FAM))\ndebug=category_genus\ntoo_big={a[0]:a[1] for a in debug if a[0]>=OLD_LABEL} \nLABEL=max([k  for k in too_big.keys()])+1\ncategory_genus=tf.sparse.SparseTensor(category_genus,[1 for _ in range(len(category_genus))],(LABEL,GEN))\nprint(genus_family)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:11:50.468922Z","iopub.execute_input":"2022-08-02T13:11:50.469804Z","iopub.status.idle":"2022-08-02T13:12:46.55898Z","shell.execute_reply.started":"2022-08-02T13:11:50.469768Z","shell.execute_reply":"2022-08-02T13:12:46.557861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b=tf.sparse.SparseTensor([[2,1]],[1],(3,2))\nprint(tf.sparse.to_dense(b))","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:12:46.561141Z","iopub.execute_input":"2022-08-02T13:12:46.561492Z","iopub.status.idle":"2022-08-02T13:12:46.586201Z","shell.execute_reply.started":"2022-08-02T13:12:46.561457Z","shell.execute_reply":"2022-08-02T13:12:46.585161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.sparse.to_dense(category_genus)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:12:46.587486Z","iopub.execute_input":"2022-08-02T13:12:46.588444Z","iopub.status.idle":"2022-08-02T13:12:46.725563Z","shell.execute_reply.started":"2022-08-02T13:12:46.588398Z","shell.execute_reply":"2022-08-02T13:12:46.724438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a=tf.sparse.sparse_dense_matmul(category_genus,tf.sparse.to_dense(genus_family))\ntf.where(a)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:12:46.728129Z","iopub.execute_input":"2022-08-02T13:12:46.728689Z","iopub.status.idle":"2022-08-02T13:12:46.765892Z","shell.execute_reply.started":"2022-08-02T13:12:46.728653Z","shell.execute_reply":"2022-08-02T13:12:46.764969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#tf.tensordot(a,tf.range(LABEL),[[1],[]])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:12:46.767945Z","iopub.execute_input":"2022-08-02T13:12:46.768312Z","iopub.status.idle":"2022-08-02T13:12:46.776152Z","shell.execute_reply.started":"2022-08-02T13:12:46.768278Z","shell.execute_reply":"2022-08-02T13:12:46.775067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.tensordot(tf.sparse.to_dense(category_genus),tf.sparse.to_dense(genus_family),[[1],[0]])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:12:46.777592Z","iopub.execute_input":"2022-08-02T13:12:46.778669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l=layers.Softmax()\n\na=tf.repeat(tf.expand_dims(tf.range(3),axis=0),2,axis=0) \na=tf.repeat(tf.expand_dims(a,axis=0),2,axis=0) \na=tf.cast(a,tf.float32)\nb=tf.repeat(tf.expand_dims(tf.range(5),axis=0),2,axis=0) \nb=tf.cast(b,tf.float32)\nl(a),l(b)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Classifier(keras.Model):\n def __init__(self,latent,metrics):\n  super(Classifier,self).__init__(self)\n  self.latent=latent\n \n  self.softmax=layers.Softmax()\n  self.full_name=layers.Dense(LABEL)\n  self.family=layers.Dense(FAM)\n  self.genus=layers.Dense(GEN) \n  self.category_genus=tf.cast(category_genus,tf.float32) \n  self.genus_family=tf.cast(genus_family,tf.float32)\n    \n  self.fam_metrics={\"fam \"+k:tf.keras.metrics.get(v) for k,v in metrics.items()}\n  self.genus_metrics={\"genus \"+k:tf.keras.metrics.get(v) for k,v in metrics.items()}\n  self.full_name_metrics={k:tf.keras.metrics.get(v) for k,v in metrics.items()} \n  #print(self.full_name_metrics)\n\n  self.side_loss=tf.keras.losses.SparseCategoricalCrossentropy()\n \n @tf.function  \n def call(self,x,training=False):\n    latent=self.latent(x,training) \n    family=self.family(latent)\n    \n    \n    attention=tf.sparse.sparse_dense_matmul(self.genus_family,family,adjoint_b=True)\n    attention=tf.transpose(attention)\n    genus=self.genus(latent)\n    genus+=attention \n    \n    \n    attention=tf.sparse.sparse_dense_matmul(self.category_genus,genus,adjoint_b=True)\n    attention=tf.transpose(attention)\n    full_name=self.full_name(latent)\n    full_name+=attention \n    \n    genus=self.softmax(genus) \n    family=self.softmax(family)\n    full_name=self.softmax(full_name) \n    return {\"full_name\":full_name,\"family\":family,\"genus\":genus}\n \n @tf.function\n def train_step(self, data):\n    # Unpack the data. Its structure depends on your model and\n    # on what you pass to `fit()`.\n    x = data[\"x\"] \n    full_name=data[\"full_name\"] \n    family=data[\"family\"] \n    genus=data[\"genus\"]\n    with tf.GradientTape() as tape:\n        d = self.call(x, training=True)  # Forward pass\n        # Compute the loss value\n        # (the loss function is configured in `compile()`)\n        fam_pred=d[\"family\"]\n        genus_pred=d[\"genus\"]\n        full_name_pred=d[\"full_name\"]\n        loss= self.compiled_loss(full_name,full_name_pred, regularization_losses=self.losses)\n        mod_loss=self.side_loss(genus,genus_pred)*0.3+self.side_loss(family,fam_pred)*0.1+loss\n        \n\n    \n    # Compute gradients\n    trainable_vars = self.trainable_variables\n    gradients = tape.gradient(mod_loss, trainable_vars)\n    # Update weights\n    self.optimizer.apply_gradients(zip(gradients, trainable_vars))\n    \n    #self.compiled_metrics.update_state(full_name, full_name_pred)\n    for k,m in self.full_name_metrics.items(): \n        m.update_state(full_name, full_name_pred) \n    for k,m in self.fam_metrics.items(): \n        m.update_state(family, fam_pred)\n    for k,m in self.genus_metrics.items(): \n        m.update_state(genus, genus_pred)\n    # Return a dict mapping metric names to current value.\n    # Note that it will include the loss (tracked in self.metrics).\n    loss_m=[m for m in self.metrics if m.name==\"loss\"][0]\n    d= {k:v.result() for d in ({\"loss\":loss_m},self.full_name_metrics,self.fam_metrics,self.genus_metrics) for k,v in d.items()}\n    d.update({\"mod_loss\":mod_loss}) \n    return d\n @tf.function\n def test_step(self,data):\n        x,y=data\n        d=self.call(x)\n        pred=d[\"full_name\"]\n        \n        for k,m in self.full_name_metrics.items(): \n            m.update_state(y,pred) \n        \n        loss=self.compiled_loss(y,pred) \n        loss_m=[m for m in self.metrics if m.name==\"loss\"][0]\n        \n        #return(self.full_name_metrics)\n        return {k:v.result() for d in ({\"loss\":loss_m},self.full_name_metrics) for k,v in d.items()}","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs=layers.Input([256,256,3]) \n\nx=layers.Rescaling(1/255)(inputs)\nx=layers.GaussianNoise(0.05)(x)\nx=layers.Conv2D(32,7,activation=\"relu\")(x)\nx=layers.Conv2D(32,5,activation=\"relu\")(x)\nx=layers.MaxPool2D()(x)\n\nx=layers.Conv2D(64,3,activation=\"relu\")(x)\nx=layers.MaxPool2D()(x)\n\nx=layers.Conv2D(64,3,activation=\"relu\")(x)\nx=layers.MaxPool2D()(x)\nX=layers.BatchNor\n\nx=layers.Conv2D(64,3,activation=\"relu\")(x)\nx=layers.MaxPool2D()(x)\n\nx=layers.Conv2D(64,3,activation=\"relu\")(x)\nx=layers.MaxPool2D()(x)\n\nx=layers.Flatten()(x) \nx=layers.Dropout(0.2)(x)\nx=layers.Dense(1024 ,activation=\"gelu\")(x)\nlatent=keras.Model(inputs,x) \n\nlatent.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model=Classifier(latent,{\"accuracy\":\"SparseCategoricalAccuracy\"})\n#model(next(val_dataset.as_numpy_iterator())[0])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T09:43:42.549258Z","iopub.execute_input":"2022-08-02T09:43:42.549646Z","iopub.status.idle":"2022-08-02T09:43:42.55384Z","shell.execute_reply.started":"2022-08-02T09:43:42.549607Z","shell.execute_reply":"2022-08-02T09:43:42.552795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"next(val_dataset.as_numpy_iterator())[0].shape","metadata":{"execution":{"iopub.status.busy":"2022-08-02T09:43:42.555349Z","iopub.execute_input":"2022-08-02T09:43:42.55601Z","iopub.status.idle":"2022-08-02T09:43:43.600851Z","shell.execute_reply.started":"2022-08-02T09:43:42.555974Z","shell.execute_reply":"2022-08-02T09:43:43.599889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Classifier(latent,{\"accuracy\":\"SparseCategoricalAccuracy\"})\nmodel.compile(optimizer=\"adam\",loss=\"sparse_categorical_crossentropy\")\nmodel.fit(train_dataset,epochs=2,steps_per_epoch=1,validation_steps=2, validation_data=val_dataset)\nmodel.evaluate(val_dataset,steps=2)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T09:43:43.602478Z","iopub.execute_input":"2022-08-02T09:43:43.602827Z","iopub.status.idle":"2022-08-02T09:43:51.651745Z","shell.execute_reply.started":"2022-08-02T09:43:43.602791Z","shell.execute_reply":"2022-08-02T09:43:51.650849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"steps=100\nval_steps=10\nval_stop=tf.keras.callbacks.EarlyStopping(restore_best_weights=True,patience=20)\nmodel_1=Classifier(latent,metrics={\"accuracy\":\"SparseCategoricalAccuracy\"})\nmodel_1.compile(optimizer=\"adam\",loss=\"sparse_categorical_crossentropy\")\nhistory=model_1.fit(train_dataset,epochs=train_size//steps,steps_per_epoch=steps,validation_steps=val_steps,\n                    validation_data=val_dataset,callbacks=[val_stop])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T09:44:33.360482Z","iopub.execute_input":"2022-08-02T09:44:33.360849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\n        fam_loss = self.compiled_loss(family,fam_pred , regularization_losses=self.losses)\n        genus_loss = self.compiled_loss(genus,genus_pred , regularization_losses=self.losses)\n        full_name_loss= self.compiled_loss(full_name,full_name_pred, regularization_losses=self.losses)\n        loss=fam_loss+genus_loss+full_name_loss\n        loss=loss/3\n\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1.save(\"big_brain\")","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:06:46.55724Z","iopub.execute_input":"2022-08-02T13:06:46.557708Z","iopub.status.idle":"2022-08-02T13:06:46.576262Z","shell.execute_reply.started":"2022-08-02T13:06:46.557647Z","shell.execute_reply":"2022-08-02T13:06:46.574776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}