-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f4f98df
Showing
18 changed files
with
720 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
% Author: Kartik Bharadwaj | ||
|
||
I = imread('download9.png'); | ||
J = rgb2gray(I); % RGB to Gray conversion | ||
|
||
%Image preprocessing | ||
|
||
K = bwareaopen(J,100,8); % Elimination of small pixel coagulation using | ||
% connected components | ||
K1 = ~K; | ||
K1 = im2uint8(K1); | ||
K1 = K1 > 135; | ||
K1 = imclearborder(K1); %Eliminates pixels which are attached to image boundary | ||
K2 = bwmorph(K1,'spur',8); | ||
K3 = bwmorph(K2,'clean'); | ||
K4 = bwmorph(K3,'bridge',Inf); | ||
|
||
%Applying filters | ||
|
||
h1 = fspecial('gaussian', 2, 1); | ||
h2 = fspecial('average', 2); | ||
g = imfilter(K4,h1, 'replicate'); | ||
g1 = imfilter(g,h2, 'replicate'); | ||
|
||
%Marking characters in Captcha | ||
[B, L, N, A] = bwboundaries(g1, 'holes'); | ||
s = regionprops(L,'all'); | ||
imshow(~g1) | ||
hold on | ||
for k = 1:length(B) | ||
boundary = B{k}; | ||
if k <= N | ||
plot(boundary(:,2), boundary(:,1)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
% Author: Kartik Bharadwaj | ||
|
||
origImg = imread('Final_test_processed.png'); | ||
[rows, columns, ColorChannels] = size(origImg); | ||
|
||
%User prompt for changing color image to grayscale | ||
|
||
if ColorChannels > 1 | ||
Message = sprintf('Your image file has %d color channels.\nDo you want to convert it to grayscale?', ColorChannels); | ||
button = questdlg(Message, 'Convert and Continue', 'Cancel'); | ||
if strcmp(button, 'Cancel') | ||
fprintf(1, 'Exited Captcha_Segmentation.m.\n'); | ||
return; | ||
end | ||
origImg = rgb2gray(origImg); | ||
end | ||
|
||
%Thresholding | ||
thresholdVal = 100; | ||
bnyImg = origImg < thresholdVal; | ||
|
||
%Getting connected component labels of the image | ||
labelImg = bwlabel(bnyImg, 8); | ||
|
||
%Getting region properties of the labels | ||
blobMeasurementsval = regionprops(labelImg, origImg, 'all'); | ||
totalBlobs = size(blobMeasurementsval, 1); | ||
|
||
imshow(origImg); | ||
axis image; | ||
hold on; | ||
boundaries = bwboundaries(bnyImg); | ||
numberOfBoundaries = size(boundaries, 1); | ||
for k = 1 : numberOfBoundaries | ||
thisBoundary = boundaries{k}; | ||
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2); | ||
end | ||
hold off; | ||
|
||
Blobareas = [blobMeasurementsval.Area]; | ||
selectedareaindex = (Blobareas > 6000) & (Blobareas < 12000); | ||
|
||
finalIndex = find(selectedareaindex); | ||
finalBlobsImg = ismember(labelImg, finalIndex); | ||
CharacterImg = bwlabel(finalBlobsImg, 8); | ||
|
||
%Image Cropping and Segmentation | ||
message = sprintf('Would you like to crop out each character to individual images?'); | ||
reply = questdlg(message, 'Extract Individual Images?', 'Yes', 'No'); | ||
if strcmpi(reply, 'Yes') | ||
figure; | ||
% Maximize window. | ||
set(gcf, 'Units','Normalized','OuterPosition',[0 0 1 1]); | ||
for k = 1 : length(finalIndex) % Loop through all blobs. | ||
% Find the bounding box of each blob. | ||
finalblobsBoundingBox = blobMeasurementsval(finalIndex(k)).BoundingBox; | ||
singlecharacter = imcrop(origImg, finalblobsBoundingBox); | ||
%imwrite(singlechar,'truth.png'); | ||
subplot(3, 4, k); | ||
imshow(singlecharacter); | ||
end | ||
end |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
function checkNNGradients(lambda) | ||
%CHECKNNGRADIENTS Creates a small neural network to check the | ||
%backpropagation gradients | ||
% CHECKNNGRADIENTS(lambda) Creates a small neural network to check the | ||
% backpropagation gradients, it will output the analytical gradients | ||
% produced by your backprop code and the numerical gradients (computed | ||
% using computeNumericalGradient). These two gradient computations should | ||
% result in very similar values. | ||
% | ||
|
||
if ~exist('lambda', 'var') || isempty(lambda) | ||
lambda = 0; | ||
end | ||
|
||
input_layer_size = 3; | ||
hidden_layer_size1 = 5; | ||
hidden_layer_size2 = 5; | ||
num_labels = 3; | ||
m = 5; | ||
|
||
% We generate some 'random' test data | ||
Theta1 = debugInitializeWeights(hidden_layer_size1, input_layer_size); | ||
Theta2 = debugInitializeWeights(hidden_layer_size2, hidden_layer_size1); | ||
Theta3 = debugInitializeWeights(num_labels, hidden_layer_size2); | ||
% Reusing debugInitializeWeights to generate X | ||
X = debugInitializeWeights(m, input_layer_size - 1); | ||
y = 1 + mod(1:m, num_labels)'; | ||
|
||
% Unroll parameters | ||
nn_params = [Theta1(:) ; Theta2(:) ; Theta3(:)]; | ||
|
||
% Short hand for cost function | ||
costFunc = @(p) nnCostFunction(p, input_layer_size, hidden_layer_size1, ... | ||
hidden_layer_size2, num_labels, X, y, lambda); | ||
|
||
[cost, grad] = costFunc(nn_params); | ||
numgrad = computeNumericalGradient(costFunc, nn_params); | ||
|
||
% Visually examine the two gradient computations. The two columns | ||
% you get should be very similar. | ||
disp([numgrad grad]); | ||
fprintf(['The above two columns you get should be very similar.\n' ... | ||
'(Left-Your Numerical Gradient, Right-Analytical Gradient)\n\n']); | ||
|
||
% Evaluate the norm of the difference between two solutions. | ||
% If you have a correct implementation, and assuming you used EPSILON = 0.0001 | ||
% in computeNumericalGradient.m, then diff below should be less than 1e-9 | ||
diff = norm(numgrad-grad)/norm(numgrad+grad); | ||
|
||
fprintf(['If your backpropagation implementation is correct, then \n' ... | ||
'the relative difference will be small (less than 1e-9). \n' ... | ||
'\nRelative Difference: %g\n'], diff); | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
function numgrad = computeNumericalGradient(J, theta) | ||
%COMPUTENUMERICALGRADIENT Computes the gradient using "finite differences" | ||
%and gives us a numerical estimate of the gradient. | ||
% numgrad = COMPUTENUMERICALGRADIENT(J, theta) computes the numerical | ||
% gradient of the function J around theta. Calling y = J(theta) should | ||
% return the function value at theta. | ||
|
||
% Notes: The following code implements numerical gradient checking, and | ||
% returns the numerical gradient.It sets numgrad(i) to (a numerical | ||
% approximation of) the partial derivative of J with respect to the | ||
% i-th input argument, evaluated at theta. (i.e., numgrad(i) should | ||
% be the (approximately) the partial derivative of J with respect | ||
% to theta(i).) | ||
% | ||
|
||
numgrad = zeros(size(theta)); | ||
perturb = zeros(size(theta)); | ||
e = 1e-4; | ||
for p = 1:numel(theta) | ||
% Set perturbation vector | ||
perturb(p) = e; | ||
loss1 = J(theta - perturb); | ||
loss2 = J(theta + perturb); | ||
% Compute Numerical Gradient | ||
numgrad(p) = (loss2 - loss1) / (2*e); | ||
perturb(p) = 0; | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
function W = debugInitializeWeights(fan_out, fan_in) | ||
%DEBUGINITIALIZEWEIGHTS Initialize the weights of a layer with fan_in | ||
%incoming connections and fan_out outgoing connections using a fixed | ||
%strategy, this will help you later in debugging | ||
% W = DEBUGINITIALIZEWEIGHTS(fan_in, fan_out) initializes the weights | ||
% of a layer with fan_in incoming connections and fan_out outgoing | ||
% connections using a fix set of values | ||
% | ||
% Note that W should be set to a matrix of size(1 + fan_in, fan_out) as | ||
% the first row of W handles the "bias" terms | ||
% | ||
|
||
% Set W to zeros | ||
W = zeros(fan_out, 1 + fan_in); | ||
|
||
% Initialize W using "sin", this ensures that W is always of the same | ||
% values and will be useful for debugging | ||
W = reshape(sin(1:numel(W)), size(W)) / 10; | ||
|
||
% ========================================================================= | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
% Initialization | ||
clear ; close all; clc | ||
|
||
% Parameters setup | ||
input_layer_size = 784; % 28x28 Input Images of Digits | ||
hidden_layer_size1 = 150; % 150 hidden units | ||
hidden_layer_size2 = 150; % 150 hidden units | ||
num_labels = 26; % 26 labels, from 1 to 26 | ||
|
||
% Load Training Data | ||
fprintf('Loading and Visualizing Data ...\n') | ||
|
||
load('letterstrain.mat'); | ||
m = size(X, 1); | ||
X = double(X); | ||
|
||
% Randomly select 100 data points to display | ||
sel = randperm(size(X, 1)); | ||
sel = sel(1:100); | ||
|
||
displayData(X(sel, :)); | ||
|
||
fprintf('Program paused. Press enter to continue.\n'); | ||
pause; | ||
|
||
fprintf('\nInitializing Neural Network Parameters ...\n') | ||
|
||
initial_Theta1 = randInitializeWeights(input_layer_size, hidden_layer_size1); | ||
initial_Theta2 = randInitializeWeights(hidden_layer_size1, hidden_layer_size2); | ||
initial_Theta3 = randInitializeWeights(hidden_layer_size2, num_labels); | ||
|
||
initial_nn_params = [initial_Theta1(:) ; initial_Theta2(:) ; initial_Theta3(:)]; | ||
|
||
fprintf('\nChecking Backpropagation... \n'); | ||
|
||
checkNNGradients; | ||
|
||
fprintf('\nProgram paused. Press enter to continue.\n'); | ||
pause; | ||
|
||
fprintf('\nChecking Backpropagation (w/ Regularization) ... \n') | ||
|
||
% Check gradients by running checkNNGradients | ||
lambda = 3; | ||
checkNNGradients(lambda); | ||
|
||
debug_J = nnCostFunction(initial_nn_params, input_layer_size, ... | ||
hidden_layer_size1, hidden_layer_size2, num_labels, X, y, lambda); | ||
|
||
fprintf(['\n\nCost at (fixed) debugging parameters (w/ lambda = %f): %f ' ... | ||
], lambda, debug_J); | ||
|
||
fprintf('Program paused. Press enter to continue.\n'); | ||
pause; | ||
|
||
fprintf('\nTraining Neural Network... \n') | ||
|
||
options = optimset('MaxIter', 800); | ||
|
||
lambda = 1; | ||
|
||
% Create "short hand" for the cost function to be minimized | ||
costFunction = @(p) nnCostFunction(p, ... | ||
input_layer_size, ... | ||
hidden_layer_size1, ... | ||
hidden_layer_size2, ... | ||
num_labels, X, y, lambda); | ||
|
||
[nn_params, cost] = fmincg(costFunction, initial_nn_params, options); | ||
|
||
Theta1 = reshape(nn_params(1:hidden_layer_size1 * (input_layer_size + 1)), hidden_layer_size1, (input_layer_size + 1)); | ||
temp1 = 1+ (hidden_layer_size1 * (input_layer_size + 1)); | ||
temp2 = (hidden_layer_size1) * (hidden_layer_size2 + 1); | ||
Theta2 = reshape(nn_params(temp1:(temp1 + temp2 - 1)),... | ||
hidden_layer_size1, (hidden_layer_size2 + 1)); | ||
Theta3 = reshape(nn_params((temp1 + temp2):end), num_labels, (hidden_layer_size2 + 1)); | ||
|
||
fprintf('Program paused. Press enter to continue.\n'); | ||
pause; | ||
|
||
load('letterstest.mat'); | ||
pred = predict(Theta1, Theta2, Theta3, double(Xtest)); | ||
|
||
fprintf('\nTraining Set Accuracy: %f\n', mean(double(pred == ytest)) * 100); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
function [h, display_array] = displayData(X, example_width) | ||
%DISPLAYDATA Display 2D data in a nice grid | ||
% [h, display_array] = DISPLAYDATA(X, example_width) displays 2D data | ||
% stored in X in a nice grid. It returns the figure handle h and the | ||
% displayed array if requested. | ||
|
||
% Set example_width automatically if not passed in | ||
if ~exist('example_width', 'var') || isempty(example_width) | ||
example_width = round(sqrt(size(X, 2))); | ||
end | ||
|
||
% Gray Image | ||
colormap(gray); | ||
|
||
% Compute rows, cols | ||
[m n] = size(X); | ||
example_height = (n / example_width); | ||
|
||
% Compute number of items to display | ||
display_rows = floor(sqrt(m)); | ||
display_cols = ceil(m / display_rows); | ||
|
||
% Between images padding | ||
pad = 1; | ||
|
||
% Setup blank display | ||
display_array = - ones(pad + display_rows * (example_height + pad), ... | ||
pad + display_cols * (example_width + pad)); | ||
|
||
% Copy each example into a patch on the display array | ||
curr_ex = 1; | ||
for j = 1:display_rows | ||
for i = 1:display_cols | ||
if curr_ex > m, | ||
break; | ||
end | ||
% Copy the patch | ||
|
||
% Get the max value of the patch | ||
max_val = max(abs(X(curr_ex, :))); | ||
display_array(pad + (j - 1) * (example_height + pad) + (1:example_height), ... | ||
pad + (i - 1) * (example_width + pad) + (1:example_width)) = ... | ||
reshape(X(curr_ex, :), example_height, example_width) / max_val; | ||
curr_ex = curr_ex + 1; | ||
end | ||
if curr_ex > m, | ||
break; | ||
end | ||
end | ||
|
||
% Display Image | ||
h = imagesc(display_array, [-1 1]); | ||
|
||
% Do not show axis | ||
axis image off | ||
|
||
drawnow; | ||
|
||
end |
Oops, something went wrong.