def predict_knn(test_bitmap_string, ncell, K=5, \ featureList=['asymmLR','asymmUD','asymmROT'], equally=False): # ncell [m,n]: break images into m x n blocks # featureList: list of strings # # load all bitmaps bitmaps = list(Drawing.objects.values_list('bitmap', flat=True)) # load corresponding list of categories categories = list(Drawing.objects.values_list('category', flat=True)) # if equally: # sample equal number of each category indices = sampling.sampleEqually(categories) bitmaps = [bitmaps[ii] for ii in indices] categories = [categories[ii] for ii in indices] # # convert bitmap strings to lists of lists trainLoL = [features.strToListList(a) for a in bitmaps] testLoL = features.strToListList(test_bitmap_string) # # centre images trainLoL = [features.centreArray(a) for a in trainLoL] testLoL = features.centreArray(testLoL) # compute cell-block counts blockTrain = [ features.cellCount(a, ncell, relative=True) for a in trainLoL ] blockTest = features.cellCount(testLoL, ncell, relative=True) # compute requested features and construct feature LoL for knn featureLoL = [] feature_test = [] for statName in featureList: if statName != 'cellCount': feat = [features.applyStat(a, statName) for a in blockTrain] featureLoL.append(feat) feat_test = features.applyStat(blockTest, statName) feature_test.append(feat_test) else: for cx in range(ncell[0]): for cy in range(ncell[1]): feat = [a[cx][cy] for a in blockTrain] featureLoL.append(feat) feat_test = blockTest[cx][cy] feature_test.append(feat_test) # transpose feature LoL: featureLoL = map(list, zip(*featureLoL)) dist_to_all = knn.distToAll(featureLoL, feature_test, 'Euclid_sq') # print(zip(categories,dist_to_all)) neighbours = knn.nearestClass(dist_to_all, categories, K) print(neighbours) prediction = knn.majorityNeighbour(neighbours) # confidence confidence = float(sum(neighbour == prediction for neighbour in neighbours)) / len(neighbours) return prediction, confidence
def silly(request): category_list = list( Category.objects.values_list('category_name', flat=True)) averages = [] for cat in category_list: bitmapStringList = list( Drawing.objects.filter(category=cat).values_list('bitmap', flat=True)) bitmapList = [features.strToListList(a) for a in bitmapStringList] # centre images bitmapList = [features.centreArray(a) for a in bitmapList] # flatten bitmapList bitmapListFlat = [[a for row in bitmap for a in row] for bitmap in bitmapList] # calculate average of bitmaps for each category sumBitmap = [ sum(a[ii] for a in bitmapListFlat) for ii in range(len(bitmapListFlat[0])) ] averages.append([float(a) / max(sumBitmap) for a in sumBitmap]) zipadee = list(zip(category_list, averages)) context = { 'zipadee': zipadee, } return render(request, 'drawing/silly.html', context)
def accuracyTest_knn(ncell, K=5, featureList=['asymmLR', 'asymmUD', 'aymmROT'], equally=False): # ncell [m,n]: break images into m x n blocks # featureList: list of strings # # load list of categories category_list = list( Category.objects.values_list('category_name', flat=True)) # load all bitmaps bitmaps = list(Drawing.objects.values_list('bitmap', flat=True)) # load corresponding list of categories categories = list(Drawing.objects.values_list('category', flat=True)) # if equally: # sample equal number of each category indices = sampling.sampleEqually(categories) bitmaps = [bitmaps[ii] for ii in indices] categories = [categories[ii] for ii in indices] # convert bitmap strings to lists of lists trainLoL = [features.strToListList(a) for a in bitmaps] # centre images trainLoL = [features.centreArray(a) for a in trainLoL] # compute cell-block counts blockTrain = [ features.cellCount(a, ncell, relative=True) for a in trainLoL ] # compute requested features and construct feature LoL for knn featureLoL = [] for statName in featureList: if statName != 'cellCount': feat = [features.applyStat(a, statName) for a in blockTrain] featureLoL.append(feat) else: for cx in range(ncell[0]): for cy in range(ncell[1]): feat = [a[cx][cy] for a in blockTrain] featureLoL.append(feat) # transpose feature LoL: featureLoL = list(map(list, zip(*featureLoL))) # perform leave-one-out test: predictions, confidences, score = knn.accuracyTest(featureLoL, categories, K=K, method='Euclid_sq') #print('K=%d: accuracy: %f' % (K,score)) return predictions, categories, confidences, score
def statPlots(request): ncell = [5, 5] category_list = list( Category.objects.values_list('category_name', flat=True)) # plot the statistics for the different categories cellCounts = [] asymmLRs = [] asymmUDs = [] asymmROTs = [] for cat in category_list: bitmapStringList = list( Drawing.objects.filter(category=cat).values_list('bitmap', flat=True)) bitmapList = [features.strToListList(a) for a in bitmapStringList] cellCount = [ features.cellCount(a, ncell, relative=True) for a in bitmapList ] cellCounts.append(cellCount) feat = [features.applyStat(a, 'asymmLR') for a in cellCount] asymmLRs.append(feat) feat = [features.applyStat(a, 'asymmUD') for a in cellCount] asymmUDs.append(feat) feat = [features.applyStat(a, 'asymmROT') for a in cellCount] asymmROTs.append(feat) colours = ['#000099', '#ff0000', '#009933', '#000000'] zipadee = list( zip(category_list, cellCounts, asymmLRs, asymmUDs, asymmROTs, colours)) mins = [] mins.append(min([x for y in asymmLRs for x in y])) mins.append(min([x for y in asymmUDs for x in y])) mins.append(min([x for y in asymmROTs for x in y])) maxs = [] maxs.append(max([x for y in asymmLRs for x in y])) maxs.append(max([x for y in asymmUDs for x in y])) maxs.append(max([x for y in asymmROTs for x in y])) context = { 'zipadee': zipadee, 'mins': mins, 'maxs': maxs, } return render(request, 'drawing/statPlots.html', context)
def retroPredict_knn(ncell, K=5, \ featureList=['asymmLR','asymmUD','aymmROT'],equally=False): # ncell [m,n]: break images into m x n blocks # featureList: list of strings # # load all bitmaps with missing prediction field test_bitmaps = list( Drawing.objects.filter(predicted='Default').values_list('bitmap', flat=True)) # load list of category names category_list = list( Category.objects.values_list('category_name', flat=True)) for test_bitmap_string in test_bitmaps: # load all bitmaps leaving out test_string bitmaps = list( Drawing.objects.exclude(bitmap=test_bitmap_string).values_list( 'bitmap', flat=True)) # load corresponding list of categories leaving out test_string categories = list( Drawing.objects.exclude(bitmap=test_bitmap_string).values_list( 'category', flat=True)) # if equally: # sample equal number of each category indices = sampling.sampleEqually(categories) bitmaps = [bitmaps[ii] for ii in indices] categories = [categories[ii] for ii in indices] # # convert bitmap strings to lists of lists trainLoL = [features.strToListList(a) for a in bitmaps] testLoL = features.strToListList(test_bitmap_string) # # centre images trainLoL = [features.centreArray(a) for a in trainLoL] testLoL = features.centreArray(testLoL) # compute cell-block counts blockTrain = [ features.cellCount(a, ncell, relative=True) for a in trainLoL ] blockTest = features.cellCount(testLoL, ncell, relative=True) # compute requested features and construct feature LoL for knn featureLoL = [] feature_test = [] for statName in featureList: if statName != 'cellCount': feat = [features.applyStat(a, statName) for a in blockTrain] featureLoL.append(feat) feat_test = features.applyStat(blockTest, statName) feature_test.append(feat_test) else: for cx in range(ncell[0]): for cy in range(ncell[1]): feat = [a[cx][cy] for a in blockTrain] featureLoL.append(feat) feat_test = blockTest[cx][cy] feature_test.append(feat_test) # transpose feature LoL: featureLoL = map(list, zip(*featureLoL)) dist_to_all = knn.distToAll(featureLoL, feature_test, 'Euclid_sq') # print(zip(categories,dist_to_all)) neighbours = knn.nearestClass(dist_to_all, categories, K) print(neighbours) prediction = knn.majorityNeighbour(neighbours) # update database with prediction d = Drawing.objects.get(bitmap=test_bitmap_string) d.predicted = prediction d.save()