Skip to content

Instantly share code, notes, and snippets.

View jschw's full-sized avatar

Julian Schweigert jschw

  • Germany
View GitHub Profile
angle = [0, math.pi/4, math.pi*2/3, math.pi*4/3]
j=0
for i in angle:
j=j+1
x_parabola, y_parabola = createParabola2(centre=[-3+j,-4+j], rotation=i)
def createParabola(centre, rotation, a=2.0, b=2.0, c=2.0):
x = np.linspace(0, 7, 100)
y_parabola = a * x**2 + b * x + c
x_parabola = 2*x
if rotation is not None:
x_parabola, y_parabola = rotateCoordinates(x_parabola, y_parabola, rotation)
x_parabola = x_parabola + centre[0]
if pmax > threshold:
# Max confidence is over threshold
# Select correct trash type
if max_index==0 or max_index==3:
# Papier
trash_type = 1
elif max_index==2 or max_index==4 or max_index==5:
# Gelber Sack
trash_type = 2
i=0
for val in plist:
plist[i] = val/img_num_mshot
i=i+1
for i in range(img_num_mshot):
img = sensor.snapshot()
lcd.display(img)
# Feed forward
fmap = kpu.forward(task, img)
plist_tmp = fmap[:]
# add values to predicition vector
i=0
img = sensor.snapshot()
fmap = kpu.forward(task, img)
plist = fmap[:]
pmax = max(plist)
labelText = ''
unknown = False
if pmax > threshold:
max_index=plist.index(pmax)
# Number of images to take in multi shot mode
img_num_mshot = 10
# Image logging
#img_logging = False
img_log_path = '/sd/saved_img'
# Note: Logging is only available in mode 0 and 1
# Path to model file
kmodel_path = '/sd/models/trash/trash.kmodel'
# Set operation mode for classification script
# 0 -> continuous/test , 1 -> single shot , 2 -> multi shot
op_mode = 0
# Image logging
img_logging = False
# Execute main script
exec(open('/sd/models/trash/trash_main_program.py').read())
def save_tflite(model, path: str):
# KPU V4 - nncase >= 0.2.0
#converter = tf.lite.TFLiteConverter.from_keras_model(model)
#tflite_model = converter.convert()
# KPU V3 - nncase = 0.1.0rc5
#model.save(path + 'weights.h5', include_optimizer=False) # <== Save model without callback flag in mode.fit
converter = tf.compat.v1.lite.TFLiteConverter.from_keras_model_file(path + 'weights.h5')
# Create callback and save the model with best (=highest) validation accuracy
checkpoint_cb = tf.keras.callbacks.ModelCheckpoint(
filepath=savepath + 'weights.h5',
monitor='val_accuracy',
save_best_only=True
)
# Create callback for saving training step info to file
csvlogger_cb = CSVLogger(savepath + 'finetune_training_log.csv', append=True, separator=';')