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 ...@@ -6,6 +6,8 @@ import numpy as np
from sample.preprocess import sliceAndMeanShift from sample.preprocess import sliceAndMeanShift
import matplotlib.pyplot as plt
''' '''
Database file structure example: Database file structure example:
...@@ -89,13 +91,14 @@ def importAndProcess(imgPath: str, dbPath: str, newImgName: str): ...@@ -89,13 +91,14 @@ def importAndProcess(imgPath: str, dbPath: str, newImgName: str):
modelsPath = baseImagePath.joinpath("Models") modelsPath = baseImagePath.joinpath("Models")
os.mkdir(modelsPath) os.mkdir(modelsPath)
# Run processing
processedResult = sliceAndMeanShift(imgArr)
# Save Slices # Save Slices
sizes = [ "128", "256", "512" ] sizes = [ "128", "256", "512" ]
for size in sizes: for size in sizes:
print("Processing size:", size)
# Run processing
processedResult = sliceAndMeanShift(imgArr, int(size))
slicedSizePath = slicedImagePath.joinpath(size) slicedSizePath = slicedImagePath.joinpath(size)
os.mkdir(slicedSizePath) os.mkdir(slicedSizePath)
...@@ -109,33 +112,34 @@ def importAndProcess(imgPath: str, dbPath: str, newImgName: str): ...@@ -109,33 +112,34 @@ def importAndProcess(imgPath: str, dbPath: str, newImgName: str):
y = sliceList[i][1] y = sliceList[i][1]
sliceArr = sliceList[i][2] sliceArr = sliceList[i][2]
sliceBaseName = "{}-{}-{}-{}-SL".format(baseImageName, size, x, y) if sliceArr.max() != 0:
sliceBasePath = slicedSizePath.joinpath(sliceBaseName) sliceBaseName = "{}-{}-{}-{}-SL".format(baseImageName, size, x, y)
os.mkdir(sliceBasePath) sliceBasePath = slicedSizePath.joinpath(sliceBaseName)
os.mkdir(sliceBasePath)
# Save Sliced Image # Save Sliced Image
sliceFilePath = sliceBasePath.joinpath("{}.{}".format(sliceBaseName, fileExt)) sliceFilePath = sliceBasePath.joinpath("{}.{}".format(sliceBaseName, fileExt))
cv2.imwrite(str(sliceFilePath), sliceArr) cv2.imwrite(str(sliceFilePath), sliceArr)
# Create Processed Slice Path # Create Processed Slice Path
sliceProcPath = sliceBasePath.joinpath("PROC") sliceProcPath = sliceBasePath.joinpath("PROC")
os.mkdir(sliceProcPath) os.mkdir(sliceProcPath)
# Segmented image and label image # Segmented image and label image
msSegArr = processedList[i][3] msSegArr = processedList[i][3]
msLabImg = processedList[i][4] msLabImg = processedList[i][4]
msSegDir = sliceProcPath.joinpath("{}-{}-{}".format(sliceBaseName, msSegDir = sliceProcPath.joinpath("{}-{}-{}".format(sliceBaseName,
"MSSEG", "MSSEG",
"PROC")) "PROC"))
msSegFileDir = msSegDir.joinpath("{}.{}".format(msSegDir.name, fileExt)) msSegFileDir = msSegDir.joinpath("{}.{}".format(msSegDir.name, fileExt))
msLabFileDir = msSegDir.joinpath("{}-{}-{}".format(sliceBaseName, msLabFileDir = msSegDir.joinpath("{}-{}-{}".format(sliceBaseName,
"MSLAB", "MSLAB",
"PROC")) "PROC"))
os.mkdir(msSegDir) os.mkdir(msSegDir)
# Save processed image and label array # Save processed image and label array
cv2.imwrite(str(msSegFileDir), msSegArr) cv2.imwrite(str(msSegFileDir), msSegArr)
np.save(str(msLabFileDir), msLabImg) np.save(str(msLabFileDir), msLabImg)
\ No newline at end of file \ No newline at end of file
import numpy as np import numpy as np
import pandas as pd
import pickle import pickle
import os import os
...@@ -61,7 +62,7 @@ class HDRModel: ...@@ -61,7 +62,7 @@ class HDRModel:
self.rfClassifer = pickle.load(open(rfPath, "rb")) 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 # Get features from VGG16 built model
features = self.__getFeatures(xTrain) features = self.__getFeatures(xTrain)
...@@ -69,19 +70,38 @@ class HDRModel: ...@@ -69,19 +70,38 @@ class HDRModel:
# TO DO: sklearn split data to train and test # TO DO: sklearn split data to train and test
xForest = features.reshape(-1, features.shape[-1]) xForest = features.reshape(-1, features.shape[-1])
yForest = yTrain.reshape(-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) dataset = pd.DataFrame(xForest)
print("Forest Train X:", xTrain.shape) dataset["Label"] = yForest
print("Forest Train Y:", yTrain.shape)
print("Forest Test X:", xTest.shape) print("BEFORE DROP VALUE 0")
print("Forest Test Y:", yTest.shape) print(dataset["Label"].unique())
self.rfClassifer.fit(xTrain, yTrain) print(dataset["Label"].value_counts())
score = self.rfClassifer.score(xTest, yTest) #dataset = dataset[dataset["Label"] != 0]
print("SCORE:", score)
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): def predict(self, xInput):
...@@ -110,6 +130,7 @@ class HDRModel: ...@@ -110,6 +130,7 @@ class HDRModel:
self.inputChannels = shape[-1] self.inputChannels = shape[-1]
vgg = VGG16(weights="imagenet", include_top=False, input_shape=shape) vgg = VGG16(weights="imagenet", include_top=False, input_shape=shape)
self.featureModel = Model(inputs=vgg.input, outputs=vgg.get_layer("block1_conv2").output) 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: ...@@ -34,8 +34,10 @@ def sliceImage(imgArr: np.array, newSize: tuple) -> dict:
for i in range(0, width, newWidth): for i in range(0, width, newWidth):
for j in range(0, height, newHeight): for j in range(0, height, newHeight):
newSliceCount += 1 newSliceCount += 1
# Create array of zeros of slice size # Create array of zeros of slice size
base = np.zeros((newWidth, newHeight, imgArr.shape[2])) base = np.zeros((newWidth, newHeight, imgArr.shape[2]))
# Slice image and broadcase to base array # Slice image and broadcase to base array
# Slice will be blacked out when outside range of original image # Slice will be blacked out when outside range of original image
sliceArr = imgArr[i:i+newWidth, j:j+newHeight, :] sliceArr = imgArr[i:i+newWidth, j:j+newHeight, :]
...@@ -66,28 +68,25 @@ def meanShiftSegmentation(imgArr: np.array, spatialRadius: int=6, rangeRadius: i ...@@ -66,28 +68,25 @@ def meanShiftSegmentation(imgArr: np.array, spatialRadius: int=6, rangeRadius: i
min_density=minDensity) min_density=minDensity)
# TO DO::POTENTIAL PROBLEM - this function holds all the results in memory, may be too much for large images # TO DO::Fix comments
# consider option to do limited what is produced def sliceAndMeanShift(imgArr: np.array, size: int):
def sliceAndMeanShift( imgArr: np.array ):
''' '''
Returns dictionary: Returns dictionary:
{ {
"slices": "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": ..., "256": ...,
"512": "512":
}, },
"meanShift": "meanShift":
{ {
"128": [ ( x, y, numRegions, segImage, labelImage ) ], "128": [ (x, y, numRegions, segImage, labelImage) ],
"256": ..., "256": ...,
"512": "512":
} }
} }
''' '''
finalResults = { finalResults = {
"slices": "slices":
...@@ -105,28 +104,19 @@ def sliceAndMeanShift( imgArr: np.array ): ...@@ -105,28 +104,19 @@ def sliceAndMeanShift( imgArr: np.array ):
} }
} }
sizes = [ 128, 256, 512 ] if imgArr.shape[0] >= size and imgArr.shape[1] >= size:
# Loop: 128, 256, 512 # Step 4: Slice
for size in sizes: sliceResults = sliceImage(imgArr, (size, size))
print( "Processing Size:", size ) # Step 5: Save slices
finalResults["slices"][str(size)] = sliceResults["slicedImages"]
if imgArr.shape[0] >= size and imgArr.shape[1] >= size: # Step 6: Mean Shift on all slices
for res in sliceResults["slicedImages"]:
# Step 4: Slice i = res[0]
sliceResults = sliceImage( imgArr, ( size, size ) ) j = res[1]
slicedImg = res[2].astype(np.uint8)
# Step 5: Save slices # mean shift
finalResults["slices"][str( size )] = sliceResults["slicedImages"] (segmentedImage, labelsImage, numberRegions) = meanShiftSegmentation(slicedImg)
# add to results
# Step 6: Mean Shift on all slices finalResults["meanShift"][str(size)].append((i, j, numberRegions, segmentedImage, labelsImage))
for res in sliceResults["slicedImages"]:
i = res[0]
j = res[1]
slicedImg = res[2].astype( np.uint8 )
#print( slicedImg.shape )
# mean shift
( segmentedImage, labelsImage, numberRegions ) = meanShiftSegmentation( slicedImg )
# add to results
finalResults["meanShift"][str( size )].append( ( i, j, numberRegions, segmentedImage, labelsImage ) )
return finalResults 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