I am using this library to scale images but when I use this code I get error:
Error: Prediction failed: CUDA out of memory. Tried to allocate 5.71 GiB (GPU 0; 14.61 GiB total capacity; 8.08 GiB already allocated; 2.04 GiB free; 11.60 GiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF
this is my code :
module.exports = async function replicateImage(imagePath) {
function imageToBase64(imagePath) {
try {
const imageBuffer = fs.readFileSync(imagePath);
const base64 = imageBuffer.toString("base64");
const mimeType = "image/png";
return `data:${mimeType};base64,${base64}`;
} catch (error) {
console.error('Error converting image to base64:', error);
return null;
}
}
async function scaleImage(imagePath) {
const replicate = new Replicate({
auth: process.env.REPLICATE_API_TOKEN
});
const model = 'nightmareai/real-esrgan:42fed1c4974146d4d2414e2be2c5277c7fcf05fcc3a73abf41610695738c1d7b';
const input = {
image: imageToBase64(imagePath),
scale: 8
};
try {
console.log('Start scaling image...');
const result = await replicate.run(model, { input });
const outputImagePath = path.join(path.dirname(imagePath), `scaled-${path.basename(imagePath)}.png`);
const response = await axios.get(result, { responseType: 'arraybuffer' });
fs.writeFileSync(outputImagePath, response.data);
console.log('Done scaling image.')
return outputImagePath;
} catch (error) {
console.error('Error scaling image:', error);
}
}
try {
return await scaleImage(imagePath);
} catch (error) {
throw error;
}
}
Can you help me ?