Dilation image-recognition algorithms cityScapes model - python

I am trying to use this image-recognition algorithms using cityScapes model
https://github.com/fyu/dilation
However, I keep on getting the following error:
- bash-4.2$ python predict.py cityscapes sunny_1336601.png --gpu 0
Using GPU 0
WARNING: Logging before InitGoogleLogging() is written to STDERR
Traceback (most recent call last):
File "predict.py", line 133, in <module>
main()
File "predict.py", line 129, in main
predict(args.dataset, args.input_path, args.output_path)
File "predict.py", line 98, in predict
color_image = dataset.palette[prediction.ravel()].reshape(image_size)
ValueError: cannot reshape array of size 12582912 into shape (1090,1920,3)
I tried reshaping the image to every common resolution I could think of, including 640x480, but I have been getting the same error.
Any help or tips is highly appreciated.
Thanks!

I don't have enough reputation to comment, so I am posting my hunch as an answer (forgive me if I'm wrong) : the given size 12582912 has to be a product of the three numbers in the tuple. A quick factorisation showed 12582912 = 1024*768*16 = 2048*1536*4 So, if the images is a 4-channel image, the resolution is 2048 x 1536 which is in standard 4:3 aspect ratio.

It turns out that Cityscapes model only takes a specific size: The width should be twice the length.

If you know Python well, you will see that ValueError is internal code error. It has nothing to do with missing dependencies or environment.
It has to do with the fact that the image was one total size first, and then it's being casted to array and then back into another dimensions.
That is not something that can be fixed or should be fixed by tempering with input data, but with addressing the bug in the provided library itself.
It is very common to have this kind of restrictions with NN classifier. Because once layers are trained, they can't be changed and the input must be very specific. Of course, it still can be "cooked" before giving it to NN but it's usually nondestructive/basic scaling, so the proportions must be preserved, which is what the library does wrong.

Related

Applying K nearest neighbors algorithm causes issue with method train

when i tried to implement k nearest Neighbors to my training datasets i have been created same as this photo
Python version 3.7.6
OpenCv version 4.2.0
enter image description here
and same as this code
but instead of training only hand written numbers i have done it for chars and numbers based on font type i have done all the steps very well and all generated arrays is perfect
only KNN.train has problem i found some posts before said it has problem with old versions of Python but at same time i heard that cv2.ml.KNearest_create() still work does ive dont something wrong
# KNN
knn = cv2.ml.KNearest_create()
knn.train(cells, cv2.ml.ROW_SAMPLE, cells_labels)
ret, result, neighbours, dist = knn.findNearest(test_cells, k=3)
it caused me strange error does it in compatible with python3.7.6
Traceback (most recent cakk kast):
File "knn-apply.py", line 38, in <module>
knn.train(cells.ml.ROW_SAMPLE, cells_labels)
RypeError: Expected Ptr<cv::UMat> for argument 'responses'

How can I write a cv2 / Pillow-modified image out to disk?

I have an image opened with Image.open(). I make some changes, reassigning pixel RGV values,and try to save.
When I try to save, I get:
Trace back (most recent call last):
File "./shimmer", line 97, in <module>
cv2.imwrite('/home/christos/portrait-out.jpg', result)
TypeError: Expected Pre<cv::UMat> for argument 'img'
This happens when the image has been created, baseline = Image.open('/home/christos/portrait-new-mini.jpg').
In searching I have found similar reported errors but no clear articulation of "Here’s what works and how’s why."
I have also seen pages in which similar work is done without similar recorded difficulties.
I can post my code if that is requested, but I am guessing that the problem is narrowly defined by the cv2.imwrite() call and something I want to be feeding the invocation.
Seems that you open the Image in Pillow and try to save the Pillow Image with OpenCV. OpenCV expect a numpy array. You should first convert your image before write in file with opencv.
Check this post : https://stackoverflow.com/a/14140796/13103631
I think, it will solve your issue

Python OpenCV - how to find ONLY rotation and translation needed to align two images given two sets of points? (no Affine, no warping)

I have two sets of matching points, eg.
# first set of points
[[696.0, 971.3333333333334], [1103.3333333333333, 934.6666666666666], ...]
# second set of points
[[475.0, 458.6666666666667], [1531.3333333333333, 524.0], ...]
from two images. Right now I'm using this piece of code to align images:
points_source = np.array(source_coordinates)
points_destination = np.array(destination_coordinates)
h, status = cv2.findHomography(points_destination, points_source, cv2.RANSAC)
aligned_image = cv2.warpPerspective(destination_image, h, (source_image.shape[1], source_image.shape[0]))
It works well most of the time, but sometimes it warps image and it aligns bad. I found estimateRigidTransform function, that'd be the best for me, because it's possible to only translate and rotate the image, but it's deprecated and when I try to use it, it throws an error:
Traceback (most recent call last):
File "align.py", line 139, in <module>
align(image, image2, source_coordinates, destination_coordinates)
File "align.py", line 111, in align
m = cv2.estimateRigidTransform(points_destination, points_source, fullAffine=False)
AttributeError: module 'cv2' has no attribute 'estimateRigidTransform'
I couldn't find any other solution than estimateRigidTransform. Is there any other function that'd work for me? Maybe I can use warpPerspective to only change rotation and translation? I don't want to use getAffineTransform function because it can accept only three points and I want to use much more points. My OpenCV version is 4.0.1-1
The function I needed is: cv2.estimateAffinePartial2D()
Instead of using plain OpenCV, I would recommend to link your project with another library that has the algorithms you are looking (for and much more). Probably the best solution would be Insight Toolkit library (ITK) or Visual Toolkit (VTK). The former is much complex and also much harder to learn, but the latter is actually very simple. They both use CMake and there is no problem in compiling/linking etc.
ITK is especially designed for image processing. It includes so called Landmark based registration, which is exactly what you need. Complete working example is available. Unfortunately, the library seems very complex at the beginning.
On the other hand, VTK also implements the same algorithm, but it can be used very simply (From the example):
vtkSmartPointer<vtkLandmarkTransform> landmarkTransform = vtkSmartPointer<vtkLandmarkTransform>::New();
landmarkTransform->SetSourceLandmarks(sourcePoints);
landmarkTransform->SetTargetLandmarks(targetPoints);
landmarkTransform->SetModeToRigidBody();
landmarkTransform->Update();
std::cout << landmarkTransform->GetMatrix() << std::endl;

Memory Error while using the Binary Opening operation in Sci-Kit Image package for granulometry

I get a memory error when I am using the opening operation in the scikit-image package (it saturates my RAM). This memory error occurs for a 3-D structuring element which is a sphere/ball of radius 16 or larger. I am trying to use granulometry to measure the size distribution of objects in the image (3D array), so I need structuring elements of increasing radii. The memory requirements also increase exponentially and I cannot find a way around it. Is there a simple solution to this problem so that I can use structuring elements of even greater radii? The image size is 200X200X200. TIA
Traceback (most recent call last):
File "R3.py", line 124, in <module>
output_image = skimage.morphology.binary_opening(image, ball)
File "/usr/lib/python2.7/dist-packages/skimage/morphology/binary.py", line 117, in binary_opening
eroded = binary_erosion(image, selem)
File "/usr/lib/python2.7/dist-packages/skimage/morphology/binary.py", line 41, in binary_erosion
ndimage.convolve(binary, selem, mode='constant', cval=1, output=conv)
File "/usr/lib/python2.7/dist-packages/scipy/ndimage/filters.py", line 696, in convolve
origin, True)
File "/usr/lib/python2.7/dist-packages/scipy/ndimage/filters.py", line 544, in _correlate_or_convolve
_nd_image.correlate(input, weights, output, mode, cval, origins)
MemoryError
A volume of dimensions 200x200x200 is pretty small. A granulometry is made of sequential openings, so you just need 2 more volumes for the computation: one temporary between the erosion and the dilation, and one more for the final results. Which means three volumes total. And the structuring element should be a list of coordinates, so nothing too big.
Consequently, there is absolutely not reason you cannot perform a granulometry on your computer for a volume of such dimensions. The only explanation for the exponential memory use would be that the intermediate results are not erased.

How to fit multiple sequences with GMMHMM?

I have a problem with the Python hmmlearn library. This is that I have several training sets and I would like to have one Gaussian mixture hmm model to fit them.
Here is an example working with multiple sequences.
X = np.concatenate([X1, X2])
lengths = [len(X1), len(X2)]
hmm.GaussianHMM(n_components=3).fit(X, lengths)
When I change GaussianHMM to GMMHMM, it returns the following error:
hmm.GMMHMM(n_components=3).fit(X, lengths)
Traceback (most recent call last):
File "C:\Users\Cody\workspace\QuickSilver_HMT\hmm_list_sqlite.py", line 141, in hmm_list_pickle
hmm.GMMHMM(n_components=3).fit(X, lengths)
File "build\bdist.win32\egg\hmmlearn\hmm.py", line 998, in fit
raise ValueError("'lengths' argument is not supported yet")
ValueError: 'lengths' argument is not supported yet
How can one fit multiple sequences with GMMHMM?
The current master version contains a re-write of GMMHMM which did not support multiple sequences at some point. Now it does, so updating should help, as #ppasler suggested.
The re-write is still a work-in-progress. Please report any issues you encounter on the hmmlearn issue tracker.

Categories

Resources