0

I'm having some issues that I'm seeking help with. I have a 13 band TIFF and a trained random forest classifier with 13 features that are in the correct order to match the 13 bands. I haven't made it to the prediction part yet. The TIFF's dimensions are (13,3426,3010). I'm trying to reshape my TIFF so it can be classified by the RFC. The very last line is the line that raises an error. In the very last line of the code sample below a ValueError "cannot reshape array of size 578994 into shape (10312260,13)" is returned. I have tried different ways of reshaping so the last two lines of code below are the most recent try.

import geopandas as gpd
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
import numpy as np
import geopandas as gpd
from sklearn.metrics import accuracy_score, classification_report
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import glob
import os
import rasterio

### There is other code here in the middle that isn't relevant ###

tifs = glob.glob(os.path.join(datafolder,'*season1.tif'))
src = rasterio.open(tifs[0])
ht,wd,crs,tform = src.height,src.width,src.crs,src.transform
src.close()


timesatStack = rasterio.open(
    os.path.join(datafolder,'timesatStack1.tif'), mode = 'w',
    driver = 'GTiff',
    height = ht,
    width = wd,
    count = 13,
    dtype = 'float32',
    crs = crs,
    transform = tform,
    compress='lzw'
    )

for ctr, image in enumerate(tifs):
    src = rasterio.open(image)
    timesatStack.write(src.read(1), ctr +1)
    src.close()

timesatStack.close()

stacked_img = rasterio.open(os.path.join(datafolder, 'timesatStack1.tif'), mode = 'r')

stacked_image = stacked_img.read()
new_shape = (stacked_img.shape[1] * stacked_img.shape[0], 13)
img_as_array = stacked_image[:, :, :13].reshape(new_shape)
2
  • It seems that you get the axis mixed. 578994 = 13*13*3426. So the expression stacked_image[:, :, :13] probably returns all bands but only a 3426x13 image for each band. The behaviour matches the docs: rasterio.readthedocs.io/en/stable/topics/… where a call to read() with no arguments returns an image where the first axis is the bands. The thing I don't understand is how new_shape = (stacked_img.shape[1] * stacked_img.shape[0], 13) is equal to (10312260, 12)?
    – Dataform
    Commented Oct 23, 2023 at 11:44
  • Thank you for your response. To start, you are correct in the '12' part of my issue. I've been going off this chapter [link] (ceholden.github.io/open-geo-tutorial/python/…). I wrote my question, and went back to trying to work my problem and tried a few other things before actually posting my question. I will come back if the axis thing doesn't work. You are fantastic though. Thank you.
    – mac754
    Commented Oct 23, 2023 at 18:14

1 Answer 1

1

I figured it out as it pertains to me.

stacked_image = np.rollaxis(stacked_img.read(), 0, 3)
new_shape = (stacked_img.shape[1] * stacked_img.shape[0], 13)
img_as_array = stacked_image[:, :, :13].reshape(new_shape)


class_prediction = clf.predict(img_as_array)
class_prediction_shpd = np.reshape(class_prediction,(-1,stacked_img.shape[1]))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.