{"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\nimport cv2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-02T17:47:02.057873Z","iopub.execute_input":"2022-09-02T17:47:02.058557Z","iopub.status.idle":"2022-09-02T17:47:08.249312Z","shell.execute_reply.started":"2022-09-02T17:47:02.058523Z","shell.execute_reply":"2022-09-02T17:47:08.248327Z"},"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-09-02T17:47:13.954068Z","iopub.execute_input":"2022-09-02T17:47:13.955114Z","iopub.status.idle":"2022-09-02T17:47:27.170881Z","shell.execute_reply.started":"2022-09-02T17:47:13.955074Z","shell.execute_reply":"2022-09-02T17:47:27.169654Z"},"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)\npretty_df=df_meta.copy()\ndf_meta.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:47:27.173053Z","iopub.execute_input":"2022-09-02T17:47:27.173499Z","iopub.status.idle":"2022-09-02T17:47:27.567993Z","shell.execute_reply.started":"2022-09-02T17:47:27.173463Z","shell.execute_reply":"2022-09-02T17:47:27.566916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_image(index,func=lambda x: x):\n    path=os.path.join(\"../input/herbarium-2022-fgvc9/train_images\",df_meta[\"path\"][index])\n    #x=plt.imread(path)/255\n    x=tf.keras.preprocessing.image.load_img(path)\n    x=x=tf.keras.utils.img_to_array(x)/255\n    x=func(x)\n    print(f'{df_meta[\"scientific_name\"][index]} shape={x.shape}')\n    plt.figure(figsize = (7,7))\n    plt.imshow(x)\n\nshow_image(20)\nLABEL=len(train_meta['categories'])\nLABEL","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:47:27.56966Z","iopub.execute_input":"2022-09-02T17:47:27.570061Z","iopub.status.idle":"2022-09-02T17:47:27.970701Z","shell.execute_reply.started":"2022-09-02T17:47:27.570027Z","shell.execute_reply":"2022-09-02T17:47:27.969756Z"},"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\"]])\n#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\n","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:47:27.972809Z","iopub.execute_input":"2022-09-02T17:47:27.973904Z","iopub.status.idle":"2022-09-02T17:47:28.659052Z","shell.execute_reply.started":"2022-09-02T17:47:27.973863Z","shell.execute_reply":"2022-09-02T17:47:28.658125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FAM=len(family_id.classes_)\nGEN=len(genus_id.classes_)\nOLD_LABEL=LABEL\nmini=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))\n#print(genus_family)","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:47:28.662234Z","iopub.execute_input":"2022-09-02T17:47:28.662519Z","iopub.status.idle":"2022-09-02T17:48:21.587764Z","shell.execute_reply.started":"2022-09-02T17:47:28.662493Z","shell.execute_reply":"2022-09-02T17:48:21.586847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size=int(len(data_df)*0.9)\n\"this made an eror thats not a good sign\"\n#val_size=len(data_df)-train_size\n\ntrain_df=data_df.sample(n=train_size)\nval_df=data_df.drop(train_df.index)\n\ntrain_df=train_df.set_index(np.arange(train_size))\nval_df=val_df.set_index(np.arange(len(val_df)))\nval_df","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:48:21.589219Z","iopub.execute_input":"2022-09-02T17:48:21.589581Z","iopub.status.idle":"2022-09-02T17:48:22.011992Z","shell.execute_reply.started":"2022-09-02T17:48:21.589545Z","shell.execute_reply":"2022-09-02T17:48:22.010906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sample(df,size):\n    return df.sample(min(size,len(df)))\n    \ndef get_samples(df,size):\n    return {i:sample(df[df[\"category\"]==i],size) for i in range(LABEL)}\nlookup=get_samples(train_df,3)\nsmall_df=pd.concat([v for k,v in lookup.items() if v.size>0])\nsmall_df","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:48:22.013492Z","iopub.execute_input":"2022-09-02T17:48:22.014237Z","iopub.status.idle":"2022-09-02T17:48:42.627662Z","shell.execute_reply.started":"2022-09-02T17:48:22.014197Z","shell.execute_reply":"2022-09-02T17:48:42.626665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"junk_df=train_df.drop(small_df.index)\nhard_df=junk_df.set_index(np.arange(len(junk_df)))\neasy_df=small_df.set_index(np.arange(len(small_df)))\neasy_df=easy_df.sample(frac=1.)","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:48:42.629408Z","iopub.execute_input":"2022-09-02T17:48:42.629832Z","iopub.status.idle":"2022-09-02T17:48:42.80849Z","shell.execute_reply.started":"2022-09-02T17:48:42.629794Z","shell.execute_reply":"2022-09-02T17:48:42.80747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HEIGHT=512\nWEITH=340\nf=tf.constant([0.35,0.45,0.2],dtype=tf.float32)\nfor i in range(2):\n    f=tf.expand_dims(f,axis=0)\nhf=tf.expand_dims(f,axis=-1)","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:48:42.809888Z","iopub.execute_input":"2022-09-02T17:48:42.810271Z","iopub.status.idle":"2022-09-02T17:48:42.817904Z","shell.execute_reply.started":"2022-09-02T17:48:42.810233Z","shell.execute_reply":"2022-09-02T17:48:42.816882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess(img):\n    img=tf.image.central_crop(img,0.8)\n    ratio=min(HEIGHT/img.shape[0],WEITH/img.shape[1])\n    img=tf.image.resize(img,[int(img.shape[0]*ratio),int(img.shape[1]*ratio)])\n    img=tf.image.resize_with_crop_or_pad(img,HEIGHT,WEITH)\n    \n    #grayed=noisey_gray(np.uint8(img*255))\n    #grayed=np.float32(grayed)\n    #grayed=tf.nn.conv2d(tf.expand_dims(grayed,0),hf,1,\"SAME\")[0].numpy()\n    grayed=tf.nn.conv2d(tf.expand_dims(img,0),hf,1,\"SAME\")[0].numpy()\n    \n    \n    x=cv2.medianBlur(grayed,5)\n    x*=255 \n    x=np.uint8(x)\n    x=cv2.Canny(x,40,125)\n    contours,heirarchies =cv2.findContours(x,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)\n    cuted=[x for x in contours if len(x)>20]\n    blank=np.zeros(img.shape,dtype=\"uint8\")\n    cv2.drawContours(blank,cuted,-1 ,(255,255,255),1)\n    \n    x=cv2.GaussianBlur(blank[:,:,0],(9,9),cv2.BORDER_DEFAULT)\n    #x=cv2.medianBlur(x,7)\n    lap=cv2.Laplacian(x,cv2.CV_64F)\n    lap=np.uint8(np.absolute(lap)) \n\n    contours,heirarchies =cv2.findContours(lap,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)\n    #400\n    cuted=[x for x in contours if len(x)>200]\n    hull=[cv2.convexHull(x) for x in cuted if len(x)>700]\n    #cuted.extend(hull)\n    lap=np.zeros(img.shape,dtype=\"uint8\")\n    #cv2.drawContours(lap,cuted,-1 ,(255,255,255),1)\n    cv2.fillPoly(lap,pts =cuted, color=(255,255,255))\n    for x in hull:\n        cv2.fillPoly(lap,pts =x, color=(255,255,255))\n    \n    mask=cv2.cvtColor(lap,cv2.COLOR_BGR2GRAY) \n    mask=np.stack([mask for _ in range(3)],axis=-1)\n    mask=tf.constant(mask,dtype=tf.bool)\n    mask=np.uint8(mask)\n    mask=tf.nn.max_pool2d(tf.expand_dims(mask,0),[7,7],1,padding=\"SAME\")[0]\n    mask=np.float32(mask)\n    x=img*mask\n    inv_mask=1-mask\n    backround=np.ones(img.shape)*0.9\n    backround*=inv_mask\n    \n    x+=backround\n    return x\n\nindex=tf.random.uniform([],0,len(df_meta)-1,dtype=tf.int64).numpy()\nshow_image(index,preprocess)","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:48:42.823142Z","iopub.execute_input":"2022-09-02T17:48:42.824028Z","iopub.status.idle":"2022-09-02T17:48:44.235033Z","shell.execute_reply.started":"2022-09-02T17:48:42.823993Z","shell.execute_reply":"2022-09-02T17:48:44.231287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"problem=\"../input/herbarium-2022-fgvc9/test_images/001/test-001001.jpg\"\ndef get_x(path):\n    x=tf.keras.preprocessing.image.load_img(path)\n    x=tf.keras.utils.img_to_array(x)/255\n    return preprocess(x)\nx=get_x(problem)\nplt.figure(figsize = (7,7))\nplt.imshow(x)","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:48:44.236624Z","iopub.execute_input":"2022-09-02T17:48:44.237026Z","iopub.status.idle":"2022-09-02T17:48:44.561765Z","shell.execute_reply.started":"2022-09-02T17:48:44.236988Z","shell.execute_reply":"2022-09-02T17:48:44.560856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#tf.keras.preprocessing.image.load_img(\"../input/herbarium-2022-fgvc9/train_images/020/07/02007__002.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:48:44.563118Z","iopub.execute_input":"2022-09-02T17:48:44.563574Z","iopub.status.idle":"2022-09-02T17:48:44.568561Z","shell.execute_reply.started":"2022-09-02T17:48:44.563537Z","shell.execute_reply":"2022-09-02T17:48:44.567643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a=tf.constant(100)\nfor i in range(4):\n    a=tf.expand_dims(a,0)\nlatent.layers[3](a)","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:48:44.569953Z","iopub.execute_input":"2022-09-02T17:48:44.570746Z","iopub.status.idle":"2022-09-02T17:48:44.914635Z","shell.execute_reply.started":"2022-09-02T17:48:44.570711Z","shell.execute_reply":"2022-09-02T17:48:44.91303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#show_image(810774)\n#show_image(111713)\n#show_image(329700)\n#show_image(115136)\n#,norm)\n#show_image(663936)\nshow_image(335377)","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:48:44.915822Z","iopub.status.idle":"2022-09-02T17:48:44.920388Z","shell.execute_reply.started":"2022-09-02T17:48:44.920122Z","shell.execute_reply":"2022-09-02T17:48:44.920149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:48:44.92437Z","iopub.status.idle":"2022-09-02T17:48:44.925192Z","shell.execute_reply.started":"2022-09-02T17:48:44.92488Z","shell.execute_reply":"2022-09-02T17:48:44.924908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#show_image(651835)\nshow_image(327086)","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:48:44.926467Z","iopub.status.idle":"2022-09-02T17:48:44.927596Z","shell.execute_reply.started":"2022-09-02T17:48:44.927348Z","shell.execute_reply":"2022-09-02T17:48:44.927371Z"},"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=get_x(path)\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)\nspecs={k:tf.TensorSpec.from_tensor(v) for k,v in b.items()}\ndef 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=get_x(path)\n    \n    return {\"x\":tf.constant(x),\"y\":tf.constant(cat,tf.int64)}\n    \nb=get_test_numbers(3)\ntest_specs={k:tf.TensorSpec.from_tensor(v) for k,v in b.items()}","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:48:57.767724Z","iopub.execute_input":"2022-09-02T17:48:57.768171Z","iopub.status.idle":"2022-09-02T17:48:58.06251Z","shell.execute_reply.started":"2022-09-02T17:48:57.76813Z","shell.execute_reply":"2022-09-02T17:48:58.06146Z"},"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-09-02T17:48:59.020506Z","iopub.execute_input":"2022-09-02T17:48:59.021856Z","iopub.status.idle":"2022-09-02T17:48:59.034017Z","shell.execute_reply.started":"2022-09-02T17:48:59.021808Z","shell.execute_reply":"2022-09-02T17:48:59.033001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(data_df.iloc[0,3])","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:48:44.931147Z","iopub.status.idle":"2022-09-02T17:48:44.931715Z","shell.execute_reply.started":"2022-09-02T17:48:44.931443Z","shell.execute_reply":"2022-09-02T17:48:44.931473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a=generator(easy_df) \nb=next(a.generate())\n{k:v.shape for k,v in b.items()}","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:49:01.079902Z","iopub.execute_input":"2022-09-02T17:49:01.080366Z","iopub.status.idle":"2022-09-02T17:49:01.217982Z","shell.execute_reply.started":"2022-09-02T17:49:01.080328Z","shell.execute_reply":"2022-09-02T17:49:01.217007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH=32\n\"naive vertion\"\ntrain_dataset=tf.data.Dataset.from_generator(generator(train_df[0:31]).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_df).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-09-02T17:49:02.115383Z","iopub.execute_input":"2022-09-02T17:49:02.115737Z","iopub.status.idle":"2022-09-02T17:49:09.581286Z","shell.execute_reply.started":"2022-09-02T17:49:02.115707Z","shell.execute_reply":"2022-09-02T17:49:09.580225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"easy_dataset=tf.data.Dataset.from_generator(generator(easy_df.sample(frac=1)).generate,output_signature=specs)\neasy_dataset=easy_dataset.batch(BATCH).prefetch(tf.data.AUTOTUNE) \n\nhard_dataset=tf.data.Dataset.from_generator(generator(hard_df).generate,output_signature=specs)\nhard_dataset=hard_dataset.batch(BATCH).prefetch(tf.data.AUTOTUNE) ","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:49:09.583401Z","iopub.execute_input":"2022-09-02T17:49:09.58378Z","iopub.status.idle":"2022-09-02T17:49:09.630988Z","shell.execute_reply.started":"2022-09-02T17:49:09.583741Z","shell.execute_reply":"2022-09-02T17:49:09.630156Z"},"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\"])\nval_dataset=tf.data.Dataset.zip((val_x,val_y)).prefetch(tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:49:09.632216Z","iopub.execute_input":"2022-09-02T17:49:09.633153Z","iopub.status.idle":"2022-09-02T17:49:09.671248Z","shell.execute_reply.started":"2022-09-02T17:49:09.633118Z","shell.execute_reply":"2022-09-02T17:49:09.670393Z"},"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-09-02T17:49:15.126914Z","iopub.execute_input":"2022-09-02T17:49:15.127554Z","iopub.status.idle":"2022-09-02T17:50:02.683949Z","shell.execute_reply.started":"2022-09-02T17:49:15.127516Z","shell.execute_reply":"2022-09-02T17:50:02.682795Z"},"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  self.g_const=1\n  self.f_const=1\n \n #@tf.function  \n def call(self,x,training=False):\n    latent=self.latent(x,training) \n    #print(\"we ran the latent part\")\n    family=self.family(latent)\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    #print(\"we ran first 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    #print(\"we ran second 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)*self.g_const+self.side_loss(family,fam_pred)*self.f_const+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":{"execution":{"iopub.status.busy":"2022-09-02T17:50:02.686188Z","iopub.execute_input":"2022-09-02T17:50:02.686604Z","iopub.status.idle":"2022-09-02T17:50:02.705297Z","shell.execute_reply.started":"2022-09-02T17:50:02.68657Z","shell.execute_reply":"2022-09-02T17:50:02.704185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"latent=tf.keras.applications.EfficientNetB1(include_top=False,input_shape=[HEIGHT,WEITH,3]\n                                           , weights='imagenet')\nlatent.layers[1].scale=1\nlatent.trainable=False","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:50:02.706831Z","iopub.execute_input":"2022-09-02T17:50:02.707551Z","iopub.status.idle":"2022-09-02T17:50:05.328331Z","shell.execute_reply.started":"2022-09-02T17:50:02.707419Z","shell.execute_reply":"2022-09-02T17:50:05.327362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs=layers.Input(latent.layers[0].input.shape[1:])\nx=latent(inputs,training=False)\nx=layers.GlobalMaxPooling2D()(x)\nx=layers.Dense(1024,activation=\"gelu\")(x)\nlatent2=tf.keras.Model(inputs,x,name=\"latent_model\")","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:50:05.330703Z","iopub.execute_input":"2022-09-02T17:50:05.331102Z","iopub.status.idle":"2022-09-02T17:50:06.147776Z","shell.execute_reply.started":"2022-09-02T17:50:05.331064Z","shell.execute_reply":"2022-09-02T17:50:06.146815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Classifier(latent2,{\"accuracy\":\"SparseCategoricalAccuracy\"})\nmodel.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001,beta_1=0.8,amsgrad=True)\n              ,loss=\"sparse_categorical_crossentropy\")\nos.mkdir(\"first_save\")\nfirst_path=os.path.join(\"first_save\",\"weights\")\nmodel.save_weights(first_path)\nmodel.fit(easy_dataset,epochs=2,steps_per_epoch=1,validation_steps=2, validation_data=val_dataset)\nmodel.evaluate(val_dataset,steps=2)\nmodel.load_weights(first_path)","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:50:06.149482Z","iopub.execute_input":"2022-09-02T17:50:06.150006Z","iopub.status.idle":"2022-09-02T17:51:15.982842Z","shell.execute_reply.started":"2022-09-02T17:50:06.149942Z","shell.execute_reply":"2022-09-02T17:51:15.981895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"steps=100\nval_steps=10\n#val_stop=tf.keras.callbacks.EarlyStopping(restore_best_weights=True,patience=20)\nhistory=model.fit(easy_dataset,epochs=len(easy_df)//(steps*BATCH),steps_per_epoch=steps,validation_steps=val_steps,\n                    validation_data=val_dataset)\n#,callbacks=[val_stop])","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:51:49.539256Z","iopub.execute_input":"2022-09-02T17:51:49.539913Z","iopub.status.idle":"2022-09-02T18:13:35.088747Z","shell.execute_reply.started":"2022-09-02T17:51:49.539876Z","shell.execute_reply":"2022-09-02T18:13:35.083848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(\"second_save\")\nsecond_path=os.path.join(\"second_save\",\"weights\")\nmodel.save_weights(second_path)","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:02:59.181229Z","iopub.status.idle":"2022-09-02T17:02:59.182024Z","shell.execute_reply.started":"2022-09-02T17:02:59.181748Z","shell.execute_reply":"2022-09-02T17:02:59.181774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"model.load_weights(\"saved_weights_neva\")","metadata":{"execution":{"iopub.status.busy":"2022-08-25T10:04:14.122264Z","iopub.execute_input":"2022-08-25T10:04:14.123387Z","iopub.status.idle":"2022-08-25T10:04:15.824319Z","shell.execute_reply.started":"2022-08-25T10:04:14.123344Z","shell.execute_reply":"2022-08-25T10:04:15.822997Z"}}},{"cell_type":"code","source":"model.load_weights(second_path)","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:02:59.18364Z","iopub.status.idle":"2022-09-02T17:02:59.185215Z","shell.execute_reply.started":"2022-09-02T17:02:59.18496Z","shell.execute_reply":"2022-09-02T17:02:59.184983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nimport shutil\n#os.rmdir(\"ziped_second\")","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:02:59.186843Z","iopub.status.idle":"2022-09-02T17:02:59.187621Z","shell.execute_reply.started":"2022-09-02T17:02:59.187363Z","shell.execute_reply":"2022-09-02T17:02:59.187389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for x in os.walk(\"second_save\"):\n    with zipfile.ZipFile(\"ziped_second.zip\",\"w\") as z:\n        for f in x[-1]:\n            z.write(os.path.join(\"second_save\",f),f)","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:02:59.189021Z","iopub.status.idle":"2022-09-02T17:02:59.189786Z","shell.execute_reply.started":"2022-09-02T17:02:59.189512Z","shell.execute_reply":"2022-09-02T17:02:59.189536Z"},"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)\n#model_1=Classifier(latent2,metrics={\"accuracy\":\"SparseCategoricalAccuracy\"})\n#model_1.compile(optimizer=\"adam\",loss=\"sparse_categorical_crossentropy\")\nhistory=model.fit(train_dataset,epochs=len(train_df)//(steps*BATCH),steps_per_epoch=steps,validation_steps=val_steps,\n                    validation_data=val_dataset,callbacks=[val_stop])","metadata":{"execution":{"iopub.status.busy":"2022-09-02T17:02:59.19121Z","iopub.status.idle":"2022-09-02T17:02:59.191991Z","shell.execute_reply.started":"2022-09-02T17:02:59.19171Z","shell.execute_reply":"2022-09-02T17:02:59.191734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}