Image Similarity with KNN

8 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$49.99
List Price:  $69.99
You save:  $20
€42.62
List Price:  €59.68
You save:  €17.05
£37.37
List Price:  £52.32
You save:  £14.95
CA$68.77
List Price:  CA$96.29
You save:  CA$27.51
A$74.96
List Price:  A$104.95
You save:  A$29.99
S$64.57
List Price:  S$90.40
You save:  S$25.83
HK$389.20
List Price:  HK$544.92
You save:  HK$155.71
CHF 39.76
List Price:  CHF 55.67
You save:  CHF 15.90
NOK kr505.36
List Price:  NOK kr707.55
You save:  NOK kr202.18
DKK kr318.41
List Price:  DKK kr445.80
You save:  DKK kr127.39
NZ$86.01
List Price:  NZ$120.42
You save:  NZ$34.41
د.إ183.58
List Price:  د.إ257.03
You save:  د.إ73.45
৳6,097.65
List Price:  ৳8,537.20
You save:  ৳2,439.55
₹4,522.64
List Price:  ₹6,332.06
You save:  ₹1,809.42
RM204.98
List Price:  RM286.99
You save:  RM82.01
₦72,486.99
List Price:  ₦101,487.59
You save:  ₦29,000.60
₨14,033.63
List Price:  ₨19,648.20
You save:  ₨5,614.57
฿1,577.18
List Price:  ฿2,208.18
You save:  ฿631
₺2,134.30
List Price:  ₺2,988.19
You save:  ₺853.89
B$270.29
List Price:  B$378.43
You save:  B$108.14
R843.07
List Price:  R1,180.37
You save:  R337.29
Лв83.37
List Price:  Лв116.72
You save:  Лв33.35
₩73,838.27
List Price:  ₩103,379.49
You save:  ₩29,541.21
₪160.14
List Price:  ₪224.21
You save:  ₪64.07
₱2,951.90
List Price:  ₱4,132.90
You save:  ₱1,181
¥7,794.32
List Price:  ¥10,912.68
You save:  ¥3,118.35
MX$901.89
List Price:  MX$1,262.72
You save:  MX$360.82
QR181.85
List Price:  QR254.61
You save:  QR72.75
P661.91
List Price:  P926.73
You save:  P264.81
KSh6,434.71
List Price:  KSh9,009.11
You save:  KSh2,574.40
E£2,376.39
List Price:  E£3,327.14
You save:  E£950.74
ብር7,769.98
List Price:  ብር10,878.60
You save:  ብር3,108.61
Kz45,598.07
List Price:  Kz63,840.95
You save:  Kz18,242.88
CLP$45,703.35
List Price:  CLP$63,988.35
You save:  CLP$18,285
CN¥352.68
List Price:  CN¥493.78
You save:  CN¥141.10
RD$3,195.38
List Price:  RD$4,473.79
You save:  RD$1,278.40
DA6,483.45
List Price:  DA9,077.34
You save:  DA2,593.89
FJ$113.56
List Price:  FJ$159
You save:  FJ$45.43
Q382.15
List Price:  Q535.05
You save:  Q152.89
GY$10,437.66
List Price:  GY$14,613.56
You save:  GY$4,175.90
ISK kr6,317.23
List Price:  ISK kr8,844.63
You save:  ISK kr2,527.40
DH458.98
List Price:  DH642.61
You save:  DH183.63
L845.14
List Price:  L1,183.26
You save:  L338.12
ден2,623.60
List Price:  ден3,673.25
You save:  ден1,049.65
MOP$399.93
List Price:  MOP$559.94
You save:  MOP$160
N$845.03
List Price:  N$1,183.11
You save:  N$338.08
C$1,836.02
List Price:  C$2,570.58
You save:  C$734.55
रु7,206.24
List Price:  रु10,089.32
You save:  रु2,883.07
S/168.17
List Price:  S/235.45
You save:  S/67.28
K211.71
List Price:  K296.42
You save:  K84.70
SAR187.57
List Price:  SAR262.62
You save:  SAR75.04
ZK1,155.56
List Price:  ZK1,617.88
You save:  ZK462.31
L216.96
List Price:  L303.76
You save:  L86.80
Kč1,032.98
List Price:  Kč1,446.26
You save:  Kč413.27
Ft16,371.24
List Price:  Ft22,921.05
You save:  Ft6,549.80
SEK kr464.23
List Price:  SEK kr649.96
You save:  SEK kr185.73
ARS$71,810.94
List Price:  ARS$100,541.06
You save:  ARS$28,730.12
Bs344.79
List Price:  Bs482.74
You save:  Bs137.94
COP$191,953.29
List Price:  COP$268,749.97
You save:  COP$76,796.67
₡24,472.34
List Price:  ₡34,263.24
You save:  ₡9,790.89
L1,313.68
List Price:  L1,839.25
You save:  L525.57
₲341,005.11
List Price:  ₲477,434.44
You save:  ₲136,429.33
$U1,961.86
List Price:  $U2,746.76
You save:  $U784.90
zł180.09
List Price:  zł252.14
You save:  zł72.05
Already have an account? Log In

Transcript

So now let's understand how we can use the knowledge of vector to do an image similarity, right. So, as you saw earlier, each image is represented as a set of three channels. And each channel has intensities between zero to 255 scale for each cell, each pixel within the image. So here in this particular example, you have one image, which is a 10 by 10 size. So, for each channel, you have turned into 10 hundred cells. And two, we can represent this as a vector and a single dimensional vector by flattening out these three channels.

So, let's start with red channel. So red channel, you start writing hundred 59 242 etc, all the way to 159 then the next value is just represents rule two starting with 222 198 147 so on and so forth. Let me also designate it in the diagram so that it's very clear. So, this is row one, then this is row two, right, so on and so forth. So, all the rows you are representing just one after the other as one cell. And you repeat that tell you Felisa red channel, right.

So here starting blue, which glitch or the restarting the next one was a race finish, that again, you just start writing the words one after another for all those and it represents the blue channel. Right, this represents similarly you can put green channels. So the first thing you Need to represent in order to represent the first thing you need to do to represent image as a vector is to flatten out all the two dimensional arrays for each other for each other into a single dimensional array just by putting the values one after another. So in terms of size, so if this was array of 10 by 10, this is array of 10 by 10 this array of 10 by 10 So, each channel will contribute 100 anyways right okay. So total size of the finals vector would be 300 into one. So, this is how you represent one image as a vector, again depending on the size of the image, your dimensions will vary.

For example, if the image is 20 by 20 So, each image will have 400 each other will have 400 cells. So, into three it will be 1200 right 1200 purposes. So, the size of the image varies depending upon the problem, but you statistics you can work basically multi dimensional image array into a single image image array before you process. So, here's another example. So, an image of one aeroplane you represent again as a 300 by one dimensional array, and again another aeroplane you can represent in another area. So this is what computer sees, right?

The computer sees just image as an array of numbers or you can also call it as a vector. So each image represents a three dimensional array vector, the size is 10 by 10. I get it the Image Size is different, your vector size will be different. So this is how you represent image as a vector. Now, if you want to calculate similarity, there are several algorithms available again you are basically computing the distance and we learned about we learned about several techniques like Euclidean distance, cosine similarity, etc, etc. Now, let's take a look at how you can apply this algorithms for an image set.

So let's say you are given an image of a dog and the dog does not have a label associated with it. And the goal is to find the label for this image right. And we are going to use an algorithm called cane it also lets k nearest neighbor. Now before giving this algorithm our computer has been trained on few images. I have pandas, cats and dogs, right. So computer already knows what a vector looks like, for any major panda.

And it is also computed internally and kept that distance c close the what distance looks like for panda. Similarly, we have images of a cat. And we have images of dogs right. So which are which we have already given the computer along with the labels. So computer knows vectors for each of these images on the corner of this slide, and also known as a load the labels for these images. So the kindergarten board is first we need to identify it's a voting system, right?

So what it does is first it we need to tell her tell the algorithm that how many neighbors you want to compute. So there's any and you can give, let's say three, four. For whatever neighbors, you want to compute and majority and then ask each one of them to vote, right, and then you just compute majority votes, and then you declare that I have similar to the majority of my neighbors. So basically, the whole idea behind gate is, if I will, if you want me to tell you who you are, you need to tell me you who your neighbors are. And then I can tell you who you are. So I am basically deriving the, your label based on what your neighbors are telling me.

That's the basic concept behind it. So in case what you do is you have all vectors for all the images, right? So you first compute a vector of the new image which does not have a label label in this particular dog. And you compute a distance for the existing set of dog images, right? And then you find nearest one of them, then you compete. The image of this topic all the images of cat, and then you similarly do that for panda.

And then what you do is you compute the let's say the neighbor satisfy you, you you, you start your neighbors you create the neighbors array based on the distance and sort them based on the distance. So, you compute five nearest neighbors based on their distance from the image under consideration. So, in this particular case, I have calculated the distance of the my current image with every other image in my corpus and the disk I have created a list and I have sorted the list based on the distance and then I have replaced the distance by associated labels. So in this particular case, the number of neighbors are five sorted in The order of ascending distance so I have three neighbors close closer which are out of five close neighbors. I have three neighbors that is it as dog one cat and panda then take out the acted.

I compute the number of words and here it says dog is the cat is one panda is one. So since dog is three majority of my neighbors are dog, so the kindergarten declares that the image is also dog. That's okay nearest nearest neighbor works. And we will also walk through a code to under understand this in more detail

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.