R Object Identification

R object identification

I usually start out with some combination of:

typeof(obj)
class(obj)
sapply(obj, class)
sapply(obj, attributes)
attributes(obj)
names(obj)

as appropriate based on what's revealed. For example, try with:

obj <- data.frame(a=1:26, b=letters)
obj <- list(a=1:26, b=letters, c=list(d=1:26, e=letters))
data(cars)
obj <- lm(dist ~ speed, data=cars)

..etc.

If obj is an S3 or S4 object, you can also try methods or showMethods, showClass, etc. Patrick Burns' R Inferno has a pretty good section on this (sec #7).

EDIT: Dirk and Hadley mention str(obj) in their answers. It really is much better than any of the above for a quick and even detailed peek into an object.

how to determine class of an object

The "correct way" to determine the S3 class of an object is using function class.

Implicit classes:

class(list(1))
#[1] "list"
class(1:5)
#[1] "integer"

Explicit classes:

class(list(1))
class(lm(Sepal.Length ~ Sepal.Width, data = iris))
#[1] "lm"

x <- 1:5
class(x) <- "myclass"
class(x)
#[1] "myclass"

Since a list can contain anything, you have to loop through it to find out the classes of the objects inside it, e.g., sapply(yourlist, class).

The class ID is stored as an attribute (as are names, dimensions and some other stuff), but usually you don't need to worry about such internals, since R offers accessor functions.

How to get the container type of an object in R?

There are three functions which will be helpful for you: mode, str and class

First, let's make some data:

nlist <- list(a=c(1,2,3), b=c("a", "b", "c"), c=matrix(rnorm(10),5))
ndata.frame <- data.frame(a=c("a", "b", "c"), b=1:3)
ncharvec <- c("a", "b", "c")
nnumvec <- c(1, 2, 3)
nintvec <- 1:3

So let's use the functions I mentioned above:

mode(nlist)
[1] "list"

str(nlist)
List of 3
$ a: num [1:3] 1 2 3
$ b: chr [1:3] "a" "b" "c"
$ c: num [1:5, 1:2] -0.9469 -0.0602 -0.3601 0.9594 -0.4348 ...

class(nlist)
[1] "list"

Now for the data frame:

mode(ndata.frame)
[1] "list"

This may surprise, you but data frames are simply a list with a data.frame class attribute.

str(ndata.frame)
'data.frame': 3 obs. of 2 variables:
$ a: Factor w/ 3 levels "a","b","c": 1 2 3
$ b: int 1 2 3
class(ndata.frame)
[1] "data.frame"

Note that there are different modes of vectors:

mode(ncharlist)
[1] "character"
mode(nnumvec)
[1] "numeric"
mode(nintvec)
[1] "numeric"

Also see that although nnumvec and nintvec appear identical, they are quite different:

str(nnumvec)
num [1:3] 1 2 3
str(nintvec)
int [1:3] 1 2 3

class(nnumvec)
[1] "numeric"
class(nintvec)
[1] "integer"

Depending on which of these you want should determine what function you use. str is a generally good function to look at variables whereas the other two are more useful in functions.

Semantic Segmentation or Object Detection?

Semantic segmentation will do you no good: The goal of semantic-segmentation is to label each pixel in the image to its semantic class. Therefore, all goats in the frame will have the same label "goat", and all sheep will have the same label "sheep".

What you might be considering is Instance-segmentation: In this task, the goal is not only to associate each pixel with its semantic class (as in semantic segmentation), but additionally to be able to set apart different instances of the same class. In your example, a good semantic segmentation will be able not only to label all goat pixels as "goats", but to separate the different goats in the frame accurately.

A very popular semantic segmentation approach is mask-RCNN: it's basically a two-stage system. First, it detects the different objects and then provides a segmentation mask for each individual bounding box. This might be a good starting point for your project: It will allow you to compare your counts based on the detection or the instance-segmentation output of the model.

Object detection with R-CNN?

R-CNN is using the following algorithm:

  1. Get region proposals for object detection (using selective search).
  2. For each region crop the area from the image and run it thorough a CNN which classify the object.

There are more advanced algorithms that are built upon this like fast-R-CNN and faster R-CNN.

fast-R-CNN:

  1. Run the entire image through the CNN
  2. For each region from the region proposals extract the area using "roi polling" layer and than classify the object.

faster R-CNN:

  1. Run the entire image through the CNN
  2. Using the features detected using the CNN find region proposals using a object proposals network.
  3. For each object proposal extract the area using "roi polling" layer and than classify the object.

There are a lot of implantation in tensorflow specifically for faster R-CNN which is the most recent variant just google faster R-CNN tensorflow.

Good luck

Training an object detection model on custom data images missing labels for other objects in the dataset. Is it possible?

It's a problem if an object shows up unlabled in a picture. The network is being given conflicting messages about what is and isn't a certain object. AFAIK there's no real way to isolate training. However, you can train on the gun dataset first and then have it run through and label all the guns in the other datasets. The same idea should work for each object you want to detect in other datasets.



Related Topics



Leave a reply



Submit