# Floating point Unet Verification - Code ```python= # -*- coding: utf-8 -*- """ Created on Thu Aug 31 01:32:21 2023 @author: Jay """ # -*- coding: utf-8 -*- """ Created on Thu Aug 31 01:32:21 2023 @author: Jay """ from os import listdir import cv2 as cv import os import numpy as np import CONV from CONV import GEMMConv3D from CONV import Conv3D from CONV import Maxpool_3D from CONV import De_Conv2x2 #from CONV import De_Conv2x2_fast import math from skimage.measure import block_reduce as maxpooling layer = ['conv'+str(i) for i in range(1, 24)] current_path = os.getcwd() all_file_name = os.listdir(current_path) print(all_file_name) parameter = {} floatingVar = {} Load_parameter = 0 if Load_parameter == 1: parameter = np.load('./parameter/parameter.npy',allow_pickle='TRUE').item() floatingVar = np.load('./parameter/floatingVar.npy',allow_pickle='TRUE').item() else: for item in all_file_name: if item in layer: file_name = os.listdir(current_path+'/'+item) print(file_name) for item_in in file_name: parameter[item+'_'+item_in[:-4]] = np.load(current_path+'/'+item+'/'+item_in) elif item.endswith("npy"): print('npy:', item) floatingVar[item[:-4]] = np.load(current_path+'/'+item) # resize the output activations for i in range(1, 32): floatingVar['out'+str(i)] = np.reshape(floatingVar['out'+str(i)], (floatingVar['out'+str(i)].shape[1],floatingVar['out'+str(i)].shape[2],floatingVar['out'+str(i)].shape[3])) print(" ==================== Unet inference (floating) ====================") Unet = {} input_data = floatingVar['input_np'][0,] out_num = 0 Unet['out'+str(out_num)] = input_data out_num += 1 weight_num = 1 # =========================================== Encoder =========================================== for times in range(5): print(out_num) # Conv + Relu Unet['out'+str(out_num)] = GEMMConv3D(Unet['out'+str(out_num-1)], floatingVar['conv'+str(weight_num)+'_weight']) Unet['out'+str(out_num)] [ Unet['out'+str(out_num)] < 0.0 ] = 0.0 out_num += 1 weight_num += 1 print(out_num) # Conv + Relu Unet['out'+str(out_num)] = GEMMConv3D(Unet['out'+str(out_num-1)], floatingVar['conv'+str(weight_num)+'_weight']) Unet['out'+str(out_num)] [ Unet['out'+str(out_num)] < 0.0 ] = 0.0 out_num += 1 weight_num += 1 if(times < 4): print(out_num) # Pooling Unet['out'+str(out_num)] = Maxpool_3D(Unet['out'+str(out_num-1)]) out_num += 1 print(out_num) print(weight_num) # =========================================== Decoder =========================================== catNum = 11 for times in range(4): print(out_num) # de-Conv Unet['out'+str(out_num)] = De_Conv2x2(Unet['out'+str(out_num-1)], floatingVar['conv'+str(weight_num)+'_weight']) out_num += 1 weight_num += 1 print(out_num) # concatenate Unet['out'+str(out_num)] = np.concatenate([Unet['out'+str(out_num-1)], Unet['out'+str(catNum)]], axis=0) out_num += 1 print(out_num) # Conv + Relu Unet['out'+str(out_num)] = GEMMConv3D(Unet['out'+str(out_num-1)], floatingVar['conv'+str(weight_num)+'_weight']) Unet['out'+str(out_num)] [ Unet['out'+str(out_num)] < 0.0 ] = 0.0 out_num += 1 weight_num += 1 print(out_num) # Conv + Relu Unet['out'+str(out_num)] = GEMMConv3D(Unet['out'+str(out_num-1)], floatingVar['conv'+str(weight_num)+'_weight']) Unet['out'+str(out_num)] [ Unet['out'+str(out_num)] < 0.0 ] = 0.0 out_num += 1 weight_num += 1 catNum -= 3 # =========================================== Output Layer =========================================== output_channel = floatingVar['conv'+str(weight_num)+'_weight'].shape[0] input_channel = floatingVar['conv'+str(weight_num)+'_weight'].shape[1] floatingVar['conv'+str(weight_num)+'_weight'] = np.reshape(floatingVar['conv'+str(weight_num)+'_weight'], (-1, floatingVar['conv'+str(weight_num)+'_weight'].shape[1])) Unet['out'+str(out_num)] = np.zeros([output_channel, Unet['out'+str(out_num-1)].shape[1], Unet['out'+str(out_num-1)].shape[2]]) outTmp = np.zeros([output_channel, Unet['out'+str(out_num-1)].shape[1], Unet['out'+str(out_num-1)].shape[2]]) for oc in range(output_channel): for ic in range(input_channel): for y in range(Unet['out'+str(out_num-1)].shape[1]): for x in range(Unet['out'+str(out_num-1)].shape[2]): Unet['out'+str(out_num)][oc, y, x] += Unet['out'+str(out_num-1)][ic, y, x] * floatingVar['conv'+str(weight_num)+'_weight'][0, ic] # =========================================== Sigmoid =========================================== myOutput = Unet['out'+str(out_num)] one = np.ones([1, 224, 224]) sigmoidOutput = one / (one + np.exp(-myOutput)) ans = floatingVar['output_np'][0, 0] error = np.abs(sigmoidOutput - ans) print('max of error = ', np.max(error)) if (np.max(error) < 1e-5): print("pass!!") else: print("failed.....") ``` - Result ![](https://hackmd.io/_uploads/H1AoTnV1a.png)