<jupyter_start><jupyter_text>Crop to BBoxes<jupyter_code>import cv2
from google.colab.patches import cv2_imshow
from PIL import Image
import matplotlib.pyplot as plt
#img = cv2.imread("/content/drive/MyDrive/KInsekten/ProtoPNet/ProtoPNet/datasets/iNat_3spec/val_Insecta/00302_Animalia_Arthropoda_Insecta_Coleoptera_Coccinellidae_Adalia_bipunctata/01703b4f-f0fd-4b47-8f79-ceff9d6778ad.jpg")
#cv2_imshow(img)

img = Image.open("/content/drive/MyDrive/KInsekten/ProtoPNet/ProtoPNet/datasets/iNat_3spec/val_Insecta/00302_Animalia_Arthropoda_Insecta_Coleoptera_Coccinellidae_Adalia_bipunctata/01703b4f-f0fd-4b47-8f79-ceff9d6778ad.jpg")
plt.imshow(img)
x, y, b, h = 201, 203, 70, 91 # index 10235 in iNat_3

#cv2_imshow(img[y : y+h, x : x+b])
plt.imshow(img.crop((x, y, x+b, y+h)))
%cd datasets/iNat_3spec
%cd ..
ind_fname = {}
#f = open('val_indexes.txt', 'r')
f = open('train_indexes.txt', 'r')

for line in f:
  ind, fname = line.split()
  ind_fname[ind] = fname
f.close()
ind_bbox = {}
#f = open('val_bboxes.txt', 'r')
f = open('train_bboxes.txt', 'r')

for line in f:
  ind, x, y, b, h = line.split()
  ind_bbox[ind] = [int(float(x)), int(float(y)), int(float(b)), int(float(h))]
f.close()
#%cd val_Insecta/
%cd train_Insecta/
import glob
specs = glob.glob('*')
specs
for spec in specs:
  files = glob.glob(spec + '/*')
  for f in files:
    try: # there was one fly removed which does not show up in the bbox list
      ind  = list(ind_fname.keys())[list(ind_fname.values()).index(f)] # get index from file name
      bbox = ind_bbox[ind] # get bbox coords from index
      x, y, b, h = tuple(bbox)

      img = Image.open(f)
      img.crop((x, y, x+b, y+h)).save('/content/drive/MyDrive/KInsekten/ProtoPNet/ProtoPNet/datasets/iNat_3spec/train_Insecta_cropped/' + f, 'JPEG')
    except:
      continue<jupyter_output><empty_output><jupyter_text>Train<jupyter_code>!pip install Augmentor
# Note: line 143 in Augmentor/Pipeline.py -> code creates folder with augmented images inside folder with source images 
#                                         -> I modified that line to keep aug. images outside source folder
import Augmentor
Augmentor.Pipeline()
!python img_aug.py
!python main.py<jupyter_output>0
training set size: 899
push set size: 899
test set size: 30
batch size: 16
start training
epoch: 	0
	warm
	train
	time: 	3.6583571434020996
	cross ent: 	0.997725902942189
	cluster: 	4.779717524846395
	separation:	5.245486976807578
	avg separation:	12.598401588306093
	accu: 		54.06006674082313%
	l1: 		60.0
	p dist pair: 	20.005292892456055
	test
	time: 	0.2967870235443115
	cross ent: 	0.704266682267189
	cluster: 	0.7895553112030029
	separation:	1.7333145141601562
	avg separation:	10.954007744789124
	accu: 		90.0%
	l1: 		60.0
	p dist pair: 	20.005292892456055
	above 70.00%
epoch: 	1
	joint
/usr/local/lib/python3.7/dist-packages/torch/optim/lr_scheduler.py:134: UserWarning: Detected call of `lr_scheduler.step()` before `optimizer.step()`. In PyTorch 1.1.0 and later, you should call them in the opposite order: `optimizer.step()` before `lr_scheduler.step()`.  Failure to do this will result in PyTorch skipping the first value of the learning rate schedule. See more details at https://pyto[...]
{'fraction_non_alphanumeric': 0.12246805858522904, 'fraction_numerical': 0.10158928014957931, 'mean_word_length': 4.376884422110553, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '410912', 'license': 'MIT', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1302, 'n_words': 297, 'path': 'ProtoPNet.ipynb', 'repo_name': 'TeodorChiaburu/ProtoPNet', 'size': '3209'}