Commit a69941f8 authored by josh's avatar josh
Browse files

check commit

parent 301a653e
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}
This diff is collapsed.
This diff is collapsed.
......@@ -6,6 +6,8 @@ import numpy as np
from sample.preprocess import sliceAndMeanShift
import matplotlib.pyplot as plt
'''
Database file structure example:
......@@ -89,13 +91,14 @@ def importAndProcess(imgPath: str, dbPath: str, newImgName: str):
modelsPath = baseImagePath.joinpath("Models")
os.mkdir(modelsPath)
# Run processing
processedResult = sliceAndMeanShift(imgArr)
# Save Slices
sizes = [ "128", "256", "512" ]
for size in sizes:
print("Processing size:", size)
# Run processing
processedResult = sliceAndMeanShift(imgArr, int(size))
slicedSizePath = slicedImagePath.joinpath(size)
os.mkdir(slicedSizePath)
......@@ -109,6 +112,7 @@ def importAndProcess(imgPath: str, dbPath: str, newImgName: str):
y = sliceList[i][1]
sliceArr = sliceList[i][2]
if sliceArr.max() != 0:
sliceBaseName = "{}-{}-{}-{}-SL".format(baseImageName, size, x, y)
sliceBasePath = slicedSizePath.joinpath(sliceBaseName)
os.mkdir(sliceBasePath)
......
import numpy as np
import pandas as pd
import pickle
import os
......@@ -61,7 +62,7 @@ class HDRModel:
self.rfClassifer = pickle.load(open(rfPath, "rb"))
def fit(self, xTrain, yTrain):
def fit(self, xTrain, yTrain, xTest=None, yTest=None):
# Get features from VGG16 built model
features = self.__getFeatures(xTrain)
......@@ -69,19 +70,38 @@ class HDRModel:
# TO DO: sklearn split data to train and test
xForest = features.reshape(-1, features.shape[-1])
yForest = yTrain.reshape(-1)
print("Forest Data X:", xForest.shape)
print("Forest Data Y:", yForest.shape)
xTrain, xTest, yTrain, yTest = train_test_split(xForest, yForest, test_size=self.trainTestSplit)
print("Forest Train X:", xTrain.shape)
print("Forest Train Y:", yTrain.shape)
dataset = pd.DataFrame(xForest)
dataset["Label"] = yForest
print("Forest Test X:", xTest.shape)
print("Forest Test Y:", yTest.shape)
self.rfClassifer.fit(xTrain, yTrain)
print("BEFORE DROP VALUE 0")
print(dataset["Label"].unique())
print(dataset["Label"].value_counts())
score = self.rfClassifer.score(xTest, yTest)
print("SCORE:", score)
#dataset = dataset[dataset["Label"] != 0]
print("AFTER DROP VALUE 0")
print(dataset["Label"].unique())
print(dataset["Label"].value_counts())
newX = dataset.drop(labels=["Label"], axis=1)
newY = dataset["Label"]
#xTrain, xTest, yTrain, yTest = train_test_split(xForest, yForest, test_size=self.trainTestSplit)
self.rfClassifer.fit(newX, newY)
print("NEW DATA")
if xTest is not None and yTest is not None:
return self.rfClassifer.score(xTest, yTest)
else:
return -1
def score(self, X, Y):
features = self.__getFeatures(X)
xForest = features.reshape(-1, features.shape[-1])
yForest = Y.reshape(-1)
return self.rfClassifer.score(xForest, yForest)
def predict(self, xInput):
......@@ -110,6 +130,7 @@ class HDRModel:
self.inputChannels = shape[-1]
vgg = VGG16(weights="imagenet", include_top=False, input_shape=shape)
self.featureModel = Model(inputs=vgg.input, outputs=vgg.get_layer("block1_conv2").output)
self.rfClassifer = RandomForestClassifier(n_estimators=50, random_state=42, verbose=1, n_jobs=-1)
self.featureModel.summary()
self.rfClassifer = RandomForestClassifier(n_estimators=50, random_state=42, verbose=1, n_jobs=3)
......@@ -34,8 +34,10 @@ def sliceImage(imgArr: np.array, newSize: tuple) -> dict:
for i in range(0, width, newWidth):
for j in range(0, height, newHeight):
newSliceCount += 1
# Create array of zeros of slice size
base = np.zeros((newWidth, newHeight, imgArr.shape[2]))
# Slice image and broadcase to base array
# Slice will be blacked out when outside range of original image
sliceArr = imgArr[i:i+newWidth, j:j+newHeight, :]
......@@ -66,28 +68,25 @@ def meanShiftSegmentation(imgArr: np.array, spatialRadius: int=6, rangeRadius: i
min_density=minDensity)
# TO DO::POTENTIAL PROBLEM - this function holds all the results in memory, may be too much for large images
# consider option to do limited what is produced
def sliceAndMeanShift( imgArr: np.array ):
# TO DO::Fix comments
def sliceAndMeanShift(imgArr: np.array, size: int):
'''
Returns dictionary:
{
"slices":
{
"128": [ ( x, y, imgArr ) ], <- x, y are pixel start positions in original image
"128": [ (x, y, imgArr) ], <- x, y are pixel start positions in original image
"256": ...,
"512":
},
"meanShift":
{
"128": [ ( x, y, numRegions, segImage, labelImage ) ],
"128": [ (x, y, numRegions, segImage, labelImage) ],
"256": ...,
"512":
}
}
'''
finalResults = {
"slices":
......@@ -105,28 +104,19 @@ def sliceAndMeanShift( imgArr: np.array ):
}
}
sizes = [ 128, 256, 512 ]
# Loop: 128, 256, 512
for size in sizes:
print( "Processing Size:", size )
if imgArr.shape[0] >= size and imgArr.shape[1] >= size:
# Step 4: Slice
sliceResults = sliceImage( imgArr, ( size, size ) )
sliceResults = sliceImage(imgArr, (size, size))
# Step 5: Save slices
finalResults["slices"][str( size )] = sliceResults["slicedImages"]
finalResults["slices"][str(size)] = sliceResults["slicedImages"]
# Step 6: Mean Shift on all slices
for res in sliceResults["slicedImages"]:
i = res[0]
j = res[1]
slicedImg = res[2].astype( np.uint8 )
#print( slicedImg.shape )
slicedImg = res[2].astype(np.uint8)
# mean shift
( segmentedImage, labelsImage, numberRegions ) = meanShiftSegmentation( slicedImg )
(segmentedImage, labelsImage, numberRegions) = meanShiftSegmentation(slicedImg)
# add to results
finalResults["meanShift"][str( size )].append( ( i, j, numberRegions, segmentedImage, labelsImage ) )
finalResults["meanShift"][str(size)].append((i, j, numberRegions, segmentedImage, labelsImage))
return finalResults
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment